The Facade pattern is intended to provide an unified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystems easier to use.
In simple terms: this pattern is very useful when you have an great amount of scattered functionality available across a couple of class libraries and you want to provide an unified global point of access for your clients.
The only thing the client has to do is to create the facade class and call its method.
|
1 2 3 4 5 6 7 8 9 10 |
class Client { public static void Main() { Facade facade = new Facade(); facade.MethodA(); Console.ReadKey(); } } |
The facade class contains all subsystem classes with the necessary methods and provides a method which calls all necessary methods from the belonging subsystem classes.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
class Facade { private FirstSubSystem m_FirstSubSystem = new FirstSubSystem(); private SecondSubSystem m_SecondSubSystem = new SecondSubSystem(); public void MethodA() { Console.WriteLine("Facade->MethodA()"); m_FirstSubSystem.MethodA(); m_SecondSubSystem.MethodB(); } } |
The subsystem classes can be anything. For the sake of simplicity in this example they provide only the desirer method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class FirstSubSystem { public void MethodA() { Console.WriteLine("* calling FirstSubSystem.MethodA"); } } class SecondSubSystem { public void MethodB() { Console.WriteLine("* calling SecondSubSystem.MethodB"); } } |
This post is part of a series on the foundational design patterns in C#. In this series we explore the ancient design patterns and their use in real-world programming situations of today. In these series of posts we explore each pattern by looking at a minimalistic example to reveal its structure and then look at more concrete and useful real-world C# code that implements the pattern.
Comments