mirror of
https://github.com/farcasclaudiu/design_pattens.git
synced 2026-06-22 07:01:29 +03:00
facade design pattern
This commit is contained in:
+3
-1
@@ -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)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace design_patterns.structural.facade
|
||||
{
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// expose simple interface
|
||||
/// hiding inside complexity
|
||||
/// </summary>
|
||||
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";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user