Facade Design Pattern In C#

Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.

If we try to understand this in simpler terms, then we can say that a room is a façade and just by looking at it from outside the door, one can not predict what is inside the room and how the room is structured from inside. Thus, Façade is a general term for simplifying the outward appearance of a complex or large system.

In software terms, Facade pattern hides the complexities of the systems and provides a simple interface to the clients.

This pattern involves one wrapper class which contains a set of methods available for the client. This pattern is particularly used when a system is very complex or difficult to understand and when the system has multiple subsystems.

Let’s see the below UML diagram,

Facade Design Pattern Using C#
Image source: Wikipedia

Here, we can see that the client is calling the Façade class which interacts with multiple subsystems making it easier for the client to interact with them.

However, it is possible that façade may provide limited functionality in comparison to working with the subsystem directly, but it should include all those features which are actually required by the client. 

For example, When someone calls the restaurant, suppose, for ordering pizza or some other food, then the operator on behalf of the restaurant gives the voice interface which is actually the façade for their customers.

Customers place their orders just by talking to the operator and they don’t need to bother about how they will prepare the pizza, what all operations will they perform, on what temperature they will cook, etc. 

Similarly, in our code sample, we can see that the client is using the restaurant façade class to order pizza and bread of different types without directly interacting with the subclasses.

Now, it's time to dive into the real code.

This is the interface specific to the pizza.

  1. public interface IPizza {  
  2.     void GetVegPizza();  
  3.     void GetNonVegPizza();  
  4. }  

This is a pizza provider class which will get pizza for their clients. Here methods can have other private methods which client is not bothered about.

  1. public class PizzaProvider: IPizza {  
  2.     public void GetNonVegPizza() {  
  3.         GetNonVegToppings();  
  4.         Console.WriteLine("Getting Non Veg Pizza.");  
  5.     }  
  6.     public void GetVegPizza() {  
  7.         Console.WriteLine("Getting Veg Pizza.");  
  8.     }  
  9.     private void GetNonVegToppings() {  
  10.         Console.WriteLine("Getting Non Veg Pizza Toppings.");  
  11.     }  
  12. }  

Similarly, this is the interface specific for the bread.

  1. public interface IBread {  
  2.     void GetGarlicBread();  
  3.     void GetCheesyGarlicBread();  
  4. }  

And this is a bread provider class.

  1. public class BreadProvider: IBread {  
  2.     public void GetGarlicBread() {  
  3.         Console.WriteLine("Getting Garlic Bread.");  
  4.     }  
  5.     public void GetCheesyGarlicBread() {  
  6.         GetCheese();  
  7.         Console.WriteLine("Getting Cheesy Garlic Bread.");  
  8.     }  
  9.     private void GetCheese() {  
  10.         Console.WriteLine("Getting Cheese.");  
  11.     }  
  12. }  

Below is the restaurant façade class, which will be used by the client to order different pizzas or breads.

  1. public class RestaurantFacade {  
  2.     private IPizza _PizzaProvider;  
  3.     private IBread _BreadProvider;  
  4.     public RestaurantFacade() {  
  5.         _PizzaProvider = new PizzaProvider();  
  6.         _BreadProvider = new BreadProvider();  
  7.     }  
  8.     public void GetNonVegPizza() {  
  9.         _PizzaProvider.GetNonVegPizza();  
  10.     }  
  11.     public void GetVegPizza() {  
  12.         _PizzaProvider.GetVegPizza();  
  13.     }  
  14.     public void GetGarlicBread() {  
  15.         _BreadProvider.GetGarlicBread();  
  16.     }  
  17.     public void GetCheesyGarlicBread() {  
  18.         _BreadProvider.GetCheesyGarlicBread();  
  19.     }  
  20. }  

Finally, below is the main method of our program,

  1. void Main() {  
  2.     Console.WriteLine("----------------------CLIENT ORDERS FOR PIZZA----------------------------\n");  
  3.     var facadeForClient = new RestaurantFacade();  
  4.     facadeForClient.GetNonVegPizza();  
  5.     facadeForClient.GetVegPizza();  
  6.     Console.WriteLine("\n----------------------CLIENT ORDERS FOR BREAD----------------------------\n");  
  7.     facadeForClient.GetGarlicBread();  
  8.     facadeForClient.GetCheesyGarlicBread();  
  9. }  

OUTPUT

Facade Design Pattern Using C#
Now, let’s see when we should use this pattern and what could be the real-life scenarios:

WHEN TO USE THIS PATTERN

Use this pattern to simplify the problem when there are multiple complex subsystems and interacting with them individually is really difficult/cumbersome.

REAL LIFE USE CASE

  • The shopkeeper is a façade for all the items in the shop.
  • Online travel portal is a façade for their customers for different holiday/travel packages.
  • Customer care is a façade for their customers for different services. 

I hope you find this article helpful. Stay tuned for more … Cheers!! 

You can also check out some of my previous articles,

Here is a more detailed article on Design Pattern in .NET


Similar Articles