diff --git a/Program.cs b/Program.cs index 859a63d..ac77235 100644 --- a/Program.cs +++ b/Program.cs @@ -11,6 +11,7 @@ using design_patterns.structural.adapter; using design_patterns.structural.bridge; using design_patterns.structural.composite; using design_patterns.structural.decorator; +using design_patterns.structural.facade; namespace design_patterns { @@ -33,7 +34,8 @@ namespace design_patterns // await AdapterSample.Run(); // await BridgeSample.Run(); // await CompositeSample.Run(); - await DecoratorSample.Run(); + // await DecoratorSample.Run(); + await FacadeSample.Run(); } catch (System.Exception ex) { diff --git a/structural/facade/FacadeSample.cs b/structural/facade/FacadeSample.cs new file mode 100644 index 0000000..b1f9f87 --- /dev/null +++ b/structural/facade/FacadeSample.cs @@ -0,0 +1,82 @@ +using System; +using System.Threading.Tasks; + +namespace design_patterns.structural.facade +{ + /// + /// Facade is a structural design pattern that provides + /// a simplified interface to a library, a framework, + /// or any other complex set of classes. + /// + /// - exposing several components through a single interface + /// - hide inner complexity + /// + public class FacadeSample + { + public static async Task Run() + { + Console.WriteLine("Structural - Facade"); + + var service = new ProcessingService(); + var result = service.ProcessData("start data"); + System.Console.WriteLine(result); + } + } + + /// + /// expose simple interface + /// hiding inside complexity + /// + public interface IProcessingService + { + string ProcessData(string input); + } + + public class ProcessingService : IProcessingService + { + private Component1 c1 = new(); + private Component2 c2 = new(); + private Component3 c3 = new(); + public string ProcessData(string input) + { + c1.LogInfo(input); + var handler = c2.StartSubsystem(input); + var res1 = c2.GetSubsystemData(handler); + c1.LogInfo(res1); + var res2 = c3.FilterInfo(res1); + c1.LogInfo(res2); + return res2; + } + + public class Component1 + { + internal void LogInfo(string input) + { + System.Console.WriteLine($"Logging: {input}"); + } + } + + public class Component2 + { + Guid handler; + internal string GetSubsystemData(Guid handler) + { + return $"{handler}_{Guid.NewGuid()}"; + } + + internal Guid StartSubsystem(string input) + { + handler = Guid.NewGuid(); + return handler; + } + } + + public class Component3 + { + internal string FilterInfo(string res1) + { + return $"{res1} with filters"; + } + } + } +} \ No newline at end of file