From 3ba755d872986f5d8b31c1bcc90f8d9e02e29b0f Mon Sep 17 00:00:00 2001 From: Claudiu Farcas Date: Tue, 20 Apr 2021 08:38:50 +0300 Subject: [PATCH] adapter sample pattern --- Program.cs | 6 ++- structural/adapter/AdapterSample.cs | 76 +++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 structural/adapter/AdapterSample.cs diff --git a/Program.cs b/Program.cs index df6fd59..1f4f950 100644 --- a/Program.cs +++ b/Program.cs @@ -7,6 +7,7 @@ using design_patterns.creational.factory; using design_patterns.creational.facadebuilder; using design_patterns.creational.singleton; using design_patterns.creational.prototype; +using design_patterns.structural.adapter; namespace design_patterns { @@ -23,7 +24,10 @@ namespace design_patterns // await FunctionalBuilderSample.Run(); // await FacadeBuilderSample.Run(); // await SingletonSample.Run(); - await PrototypeSample.Run(); + // await PrototypeSample.Run(); + + // structural + await AdapterSample.Run(); } catch (System.Exception ex) { diff --git a/structural/adapter/AdapterSample.cs b/structural/adapter/AdapterSample.cs new file mode 100644 index 0000000..f536f75 --- /dev/null +++ b/structural/adapter/AdapterSample.cs @@ -0,0 +1,76 @@ +using System; +using System.Threading.Tasks; + +namespace design_patterns.structural.adapter +{ + /// + /// Adapter is a structural design pattern that + /// allows objects with incompatible interfaces to collaborate. + /// + /// This is a special object that converts the interface + /// of one object so that another object can understand it. + /// + /// An adapter wraps one of the objects to hide the complexity + /// of conversion happening behind the scenes. + /// The wrapped object isn’t even aware of the adapter. + /// + /// It’s very often used in systems based on some legacy code. + /// + public class AdapterSample + { + public static async Task Run() + { + Console.WriteLine("Structural - Adapter"); + + var person = new Person { + ID = Guid.NewGuid().ToString(), + Name = "Test person" + }; + ExternalService service = new ExternalService(); + + ExternalServiceAdapter adapter = new ExternalServiceAdapter(); + var status = adapter.GetStatus(person, service); + + Console.WriteLine($"Status of {status.ID} - {status.PersonStatus}"); + } + } + + // client DDD models - we don't want to modify their structure + // to match external system and inherit unwanted data and behavior + public class Person { + public string Name { get; set; } + public string ID { get; set; } + } + public class PersonInfo { + public string ID { get; set; } + public string PersonStatus { get; set; } + } + + // defined inside client DDD core + // interface for adapting external service + public interface IExternalService { + PersonInfo GetStatus(Person person, ExternalService service); + } + + // (legacy) external Service + public class ExternalService { + public string GetPersonStatus(string id) { + return $"Status OK"; + } + } + + // adapter implementation, we can have + // multiple adapter implementations if needed + public class ExternalServiceAdapter : IExternalService + { + public PersonInfo GetStatus(Person person, ExternalService service) + { + return new PersonInfo { + ID = person.ID, + PersonStatus = service.GetPersonStatus(person.ID) + }; + } + } + + +} \ No newline at end of file