Facade Design Pattern

Introduction 

 
Let's understand why we need a facade design pattern. Take a sly look at an image below. Right-hand side class only needs to call four methods. But we'll likely need to examine and dig around in the internals of mud class (leftmost classA) to determine which four they are. or I can place a facade between my classes andn now the facade has an internal reference to the classA and orchestrates calls on behalf of our program. We're using a facade class to contain the classA and only expose the methods needed by our program. We can use more meaningful method names plus our program only talks to the facade without knowing about the classA at all.
 
 
 
The complex project obviously has hundreds of classes. This pattern assist our calling class by focusing on what is needed rather than going through everything, (abstraction-oops).
 
 
Assume we want to implement a smartphone class that has 3 specifications: Processor, Memory(RAM, ROM) & Display. Our implementation might look like this:
 
1st class Processor
  1. namespace FacadeDesignPattern  
  2. {  
  3.     public class Processor  
  4.     {  
  5.         public void DisplayProcessor()  
  6.         {  
  7.             System.Console.WriteLine("Snapdragon 635");  
  8.         }  
  9.   
  10.     }  
  11. }  
2nd class Memory :
  1. namespace FacadeDesignPattern  
  2. {  
  3.     public class Memory  
  4.     {  
  5.         public void AssembleMemory()  
  6.         {  
  7.             System.Console.WriteLine("8GB RAM + 128GB ROM");  
  8.         }  
  9.     }  
  10. }  
3rd class Display:
  1. namespace FacadeDesignPattern  
  2. {  
  3.     public class Display  
  4.     {  
  5.         public void AssembleDisplay()  
  6.         {  
  7.             System.Console.WriteLine("Retina Display");  
  8.         }  
  9.     }  
  10. }  
Now our calling class SmartPhone:
  1. using System;  
  2.   
  3. namespace FacadeDesignPattern  
  4. {  
  5.     class SmartPhone  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("One Plus 8");  
  10.             //calling processor  
  11.             Processor processor = new Processor();  
  12.             processor.AssembleProcessor();  
  13.   
  14.             //calling memory  
  15.             Memory memory = new Memory();  
  16.             memory.AssembleMemory();  
  17.   
  18.             //calling display  
  19.             Display display = new Display();  
  20.             display.AssembleDisplay();  
  21.         }  
  22.     }  

Let's first understand, this is a simple problem, but as smartphone needs to follow the latest trend and innovation you'll need to add more classes for every feature.

You'll have to go through all of the mud of classes to reImplement smartPhone. Here we only have 3 classes (Display, Memory, Processor) but in a real-life project, as I said there would be hundreds of classes which make it difficult to look for classes which are needed to implement a new smartPhone.we can add a middle layer which acts as a facade for our smartphone, which will take care of all the functionalities that needed to make a smartphone. To make our pattern more advance we can add one interface through which our smartPhone class will interact.
 
Why interface? Good question. This abstraction allows the calling program (smartPhone in our case) to work solely with the facade and simplifies the code. Introducing an interface here really tidies things up for the caller and allows easily adding new facade classes to the underlying service classes(more feature more classes) change in the future (more advanced smartphone classes in the future).
 
If tomorrow, I want to release new smartPhone for Android 10 with new high-end specifications, that can be achieved by interacting with an interface.
 
Having said that, let's start working with the Facade Design Pattern.

What if we put processor, memory, display behind the facade so smartphone doesn't have to worry about above complexity.
 
Step 1
 
Let's add interface ISmartPhoneFacade
  1. namespace FacadeDesignPattern  
  2. {  
  3.     interface ISmartPhoneFacade  
  4.     {  
  5.        void ShowSmartPhoneSpecifications();  
  6.     }  

Step 2
 
Now let's add class SmartPhoneFacade
  1. namespace FacadeDesignPattern  
  2. {  
  3.     public class SmartPhoneFacade : ISmartPhoneFacade  
  4.     {  
  5.         private Display display;  
  6.         private Memory memory;  
  7.         private Processor processor;  
  8.   
  9.         public SmartPhoneFacade() : this(new Display(), new Memory(), new Processor())  
  10.         {  
  11.   
  12.         }  
  13.         public SmartPhoneFacade(Display display, Memory memory, Processor processor)  
  14.         {  
  15.             this.memory = memory;  
  16.             this.display = display;  
  17.             this.processor = processor;  
  18.         }  
  19.         public void ShowSmartPhoneSpecifications()  
  20.         {  
  21.             display.AssembleDisplay();  
  22.             memory.AssembleMemory();  
  23.             processor.DisplayProcessor();  
  24.         }  
  25.     }  

Now a simpler look at our smartPhone class:
  1. using System;  
  2.   
  3. namespace FacadeDesignPattern  
  4. {  
  5.     class SmartPhone  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             ISmartPhoneFacade smartPhone = new SmartPhoneFacade();  
  10.             smartPhone.ShowSmartPhoneSpecifications();  
  11.         } 
  12.     }  

If tomorrow, the new smartPhone device comes all I have to do is create a new facade, specific to that smartPhone features and inherit from interface ISmartPhoneFacade. This is the power of ABSTRACTION in oops.
 
Note: this might feels like a headache to add an extra class, but this helps in the bigger picture.
 
I sincerely hope you enjoyed this blog and that you're inspired to apply what you've learned to your own applications.
 
Connect with me:


Similar Articles