Facade design pattern
Facade pattern is a good design pattern when there is a large system with large number of interdependent classes.
This pattern helps us to hide the complexities of the large system and provide a simple interface for the client applications. Its wrapper class used to hide the implementation details.
Let’s take an example of ‘Production support team’ activities.
The client is reporting an issue and raising a problem ticket. The issue probably is described in one line.
The issue will be assigned to the support person and he/she will identify the root cause and will provide a workaround after a while.
Most of the case the client doesn’t care/ doesn’t wants to know what are the steps followed to identify the issue, who worked on it, how many teams involved, and how many test cases we used to test the workaround. Only client wants is the issue to be fixed ASAP.
Please have a look into the below diagram of production support team
In the above the diagram, the client may be a user who created a ticket to report an issue.
The Facade will provide various simplified access to the support services from the interdependent subsystems which are considered as complicated.
Subsystems are hidden from the client. Client may doesn’t have direct access to the subsystems (for example)
Let’s see the code example below;
Sub Systems
Here the 2 classes sees independent first as they can do certain tasks individually.
- using System;
- namespace facade
- {
- class Support_SubSytem
- {
- public void connectSession() { Console.WriteLine("Support Session Connected"); }
- public void sendSummary() { Console.WriteLine("Support send summary");
- public void escalateRRT() { Console.WriteLine("escalate to RRT"); }
- public void resolve() { Console.WriteLine("Resolve the issue"); }
- }
-
- class RRT_SubSystem
- {
- public void connectSession() { Console.WriteLine("RRT Session Connected"); }
- public void sendSummary() { Console.WriteLine("RRT send summary"); }
- public void debugCode() { Console.WriteLine("debug Code"); }
- public void escalateDEV() { Console.WriteLine("escalate to DEV"); }
- }
- }
Facade
In our example, façade class has the activities that we will perform to fix the issues.
In order to fix the issue we may need to follow a specific sequence of activities.
- class Ticket_Facade
- {
- Support_SubSytem supportsystem = new Support_SubSytem();
- RRT_SubSystem rrtsubsystem = new RRT_SubSystem();
- public void fixMyIssue()
- {
-
- supportsystem.connectSession();
- supportsystem.sendSummary();
- supportsystem.escalateRRT();
- rrtsubsystem.debugCode();
- rrtsubsystem.connectSession();
- rrtsubsystem.sendSummary();
- supportsystem.resolve();
- }
- }
Main function
The main function act as the client and it can be used to call the fixMyIssue() in the Ticket_Facade class
- class Client
- {
- static void Main(string[] args)
- {
- Ticket_Facade facade = new Ticket_Facade();
- Console.WriteLine("Facade Example\n----------------");
- facade.fixMyIssue();
- Console.ReadKey();
- }
- }
OUTPUT