Please enable Javascript to correctly display the contents on Dot Net Tricks!

Facade Design Pattern - C#

  Author : Shailendra Chauhan
Updated On : 26 Sep 2016
Total Views : 54,456   
Support : All C# & .Net
 

Facade 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 system has a large number of interdependent classes or its source code is unavailable. In this article, I would like 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 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 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 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 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 system has a large number of interdependent classes or its source code is unavailable

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.

YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
14 DEC
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM- 09:00 AM IST

Know More
5 DEC
AngularJS Development (online)

Mon - Fri     6:30 AM-7:30 AM IST

3 DEC
AngularJS Development (offline)

SAT,SUN     11:00 AM-12:30 PM IST

3 DEC
MEAN Stack Development (offline)

Sat, Sun     (09:30 AM-11:00 AM IST)

26 NOV
ASP.NET MVC with AngularJS Development (offline)

(SAT,SUN)     03:30 PM-05:00 PM IST

24 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

12 NOV
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

3 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+