Shed Light on Facade Pattern Just an Interface

Façade means “face or front”. A Facade Pattern works as the name implies.

A definition as given below is referenced from dofactory.

  • Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

  • Wrap a complicated subsystem with a simpler interface.

In layman terms “façade provides an interface to interact with sub layers to provide the desired result without interacting with sub layers directly from the console”.

UML class diagram

facade

I have used a very basic and daily used real life example to show the case. A Mobile shop.

Whenever a customer enters a mobile shop and asks about mobile phones of a specific brand such as Samsung but under the budget (the budget could vary) . Please have a look at the image given below:

customer image

As in the image shown above, the customer is asking for a phone of the brand Samsung under 15000 INR. He just asks for an interface/façade layer “shopkeeper”. It doesn't matter for him what happens behind the scenes but he wants the mobile as desired.

I hope the scenario is clear.

Now we jump into a code segment and understand it practically.

This is the typical class diagram for the Façade pattern:

facade layer

The classes, interfaces and objects in the preceding class diagram can be identified as follows:

Stock and Price: Subsystems.

MobileFacade: Facade class.

Structural code in C#

This structural code shows the Facade pattern that provides a simplified and uniform interface to a large subsystem of classes.
The subsystem classes Stock and Price code are depicted below:

  1. public class Price  
  2. {  
  3.       public string GetMobileNameUnderPrice(string objMobileName,int price)  
  4.       {  
  5.             switch (price)  
  6.             {  
  7.                   case 8000:  
  8.                   {  
  9.                         return objMobileName+ " Trend";   
  10.                   }  
  11.   
  12.                   case 10000:  
  13.                   {  
  14.                         return objMobileName+ " Ace";  
  15.                   }  
  16.                   case 15000:  
  17.                   {  
  18.                         return objMobileName+ " Grand";  
  19.                   }  
  20.                   case 25000:  
  21.                   {  
  22.                         return objMobileName+ " S3";  
  23.                   }  
  24.                   default:  
  25.                   return objMobileName+" Guru";  
  26.                   break;  
  27.             }  
  28.       }  
  29. }  
  30.   
  31. class Stock  
  32. {  
  33.       public bool GetStock()  
  34.       {  
  35.             return true;  
  36.       }  
Kindly have a look at the MobileFacade.cs:
  1. public class Price  
  2. {  
  3.       public string GetMobileNameUnderPrice(string objMobileName,int price)  
  4.       {  
  5.             switch (price)  
  6.             {  
  7.                   case 8000:  
  8.                   {  
  9.                         return objMobileName+ " Trend";   
  10.                   }  
  11.                     
  12.                   case 10000:  
  13.                   {  
  14.                         return objMobileName+ " Ace";  
  15.                   }  
  16.                   case 15000:  
  17.                   {  
  18.                         return objMobileName+ " Grand";  
  19.                   }  
  20.                   case 25000:  
  21.                   {  
  22.                         return objMobileName+ " S3";  
  23.                   }  
  24.                   default:  
  25.                   return objMobileName+" Guru";  
  26.                   break;  
  27.             }  
  28.       }  
  29. }  
  30.   
  31. class Stock  
  32. {  
  33.       public bool GetStock()  
  34.       {  
  35.             return true;  
  36.       }  
  37. }  
If you run your application then it asks you to give some amount value so that the façade can determine the mobile under the price you have provided:

run application

The output will be as shown below:

application output

Pros 
  • The main advantage of this pattern ist that you only interact with an interface to get the desired result. What happens behind the scene doesn't matter.

  • There will be an entry point to each level of layered software.
Cons
  • A tightly coupled system. The entry point is dependent on sub layers.

  • Façade layers need to create many objects of subclasses internally to get the desired output.
Disclaimer: These are all some of my determinations that I have shared with you. Please touch base with me if you feel any query or suggestion for improvement.

I wish it will help you utilize both features at best.

To learn more about MVC please go to the following link.

MVC Articles

Thanks.

Enjoy coding and reading.


Similar Articles