Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Facade Design Pattern

Facade Design Pattern

23 Aug 2022
Intermediate
151K Views
4 min read
Learn via Video Course & by Doing Hands-on Labs

⭐ .NET Design Patterns Course: Design Patterns in C# Online Training

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?

What is facade Pattern

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.

Facade Pattern - UML Diagram & Implementation

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:

  1. Complex System

    A library of subsystems.

  2. SubsystemA, SubsystemB, SubsystemC

    These are classes within a complex system and offer detailed operations.

  3. Façade

    This is a wrapper class which wrapper class which contains a set of members which are required by the client.

  4. Client

    This is a class which calls the high-level operations in the Façade.

C# - Implementation Code

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());
 }
}

Façade Pattern - Example

Who is what?

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

  1. CarModel, CarEngine, CarBody, CarAccessories - These are subsystems.

  2. CarFacade- Facade class.

C# - Sample Code

/// <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();

 }
}

facade Pattern Demo - Output

When to use it?

  1. A simple interface is required to access to a complex system.

  2. The abstractions and implementations of a subsystem are tightly coupled.

  3. Need an entry point to each level of layered software.

  4. 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

Read More Articles Related to Design Pattern
What do you think?

I hope you will enjoy the Facade Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this