Facade Design pattern falls under Structural Pattern of Gang of Four (GOF) Design Patterns in .Net. The Facade design pattern is particularly used when a system is very complex or difficult to understand because the system has a large number of interdependent classes or its source code is unavailable. In this article, I would like to share what is facade pattern and how is it work?
Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.
This pattern involves a single wrapper class which contains a set of members which are required by the client. These members access the system on behalf of the facade client and hide the implementation details.
The facade design pattern is particularly used when a system is very complex or difficult to understand because the system has a large number of interdependent classes or its source code is unavailable.
The UML class diagram for the implementation of the facade design pattern is given below:
The classes, interfaces, and objects in the above UML class diagram are as follows:
A library of subsystems.
These are classes within a complex system and offer detailed operations.
This is a wrapper class which wrapper class which contains a set of members which are required by the client.
This is a class which calls the high-level operations in the Façade.
class SubsystemA { public string OperationA1() { return "Subsystem A, Method A1\n"; } public string OperationA2() { return "Subsystem A, Method A2\n"; } } class SubsystemB { public string OperationB1() { return "Subsystem B, Method B1\n"; } public string OperationB2() { return "Subsystem B, Method B2\n"; } } class SubsystemC { public string OperationC1() { return "Subsystem C, Method C1\n"; } public string OperationC2() { return "Subsystem C, Method C2\n"; } } public class Facade { SubsystemA a = new SubsystemA(); SubsystemB b = new SubsystemB(); SubsystemC c = new SubsystemC(); public void Operation1() { Console.WriteLine("Operation 1\n" + a.OperationA1() + a.OperationA2() + b.OperationB1()); } public void Operation2() { Console.WriteLine("Operation 2\n" + b.OperationB2() + c.OperationC1() + c.OperationC2()); } }
The classes, interfaces, and objects in the above class diagram can be identified as follows:
CarModel, CarEngine, CarBody, CarAccessories - These are subsystems.
CarFacade- Facade class.
/// <summary> /// The 'Subsystem ClassA' class /// </summary> class CarModel { public void SetModel() { Console.WriteLine(" CarModel - SetModel"); } } /// <summary> /// The 'Subsystem ClassB' class /// </summary> class CarEngine { public void SetEngine() { Console.WriteLine(" CarEngine - SetEngine"); } } /// <summary> /// The 'Subsystem ClassC' class /// </summary> class CarBody { public void SetBody() { Console.WriteLine(" CarBody - SetBody"); } } /// <summary> /// The 'Subsystem ClassD' class /// </summary> class CarAccessories { public void SetAccessories() { Console.WriteLine(" CarAccessories - SetAccessories"); } } /// <summary> /// The 'Facade' class /// </summary> public class CarFacade { CarModel model; CarEngine engine; CarBody body; CarAccessories accessories; public CarFacade() { model = new CarModel(); engine = new CarEngine(); body = new CarBody(); accessories = new CarAccessories(); } public void CreateCompleteCar() { Console.WriteLine("******** Creating a Car **********\n"); model.SetModel(); engine.SetEngine(); body.SetBody(); accessories.SetAccessories(); Console.WriteLine("\n******** Car creation complete **********"); } } /// <summary> /// Facade Pattern Demo /// </summary> class Program { static void Main(string[] args) { CarFacade facade = new CarFacade(); facade.CreateCompleteCar(); Console.ReadKey(); } }
A simple interface is required to access to a complex system.
The abstractions and implementations of a subsystem are tightly coupled.
Need an entry point to each level of layered software.
The facade design pattern is particularly used when a system is very complex or difficult to understand because the system has a large number of interdependent classes or its source code is unavailable