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

Facade Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 14 Jul 2013
Total Views : 65,939   
Updated On : 26 Sep 2016
 

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.



Free Interview Books
 
COMMENTS (0)
5 AUG
ASP.NET MVC with Angular4 Development (Online)

10:30 Am to 12:30 PM IST (+ 5:30 GMT)

Know More
3 AUG
ASP.NET Core with Angular4 Development (Online)

07:00 AM - 9:00 AM IST(+5:30 GMT)

Know More
29 JUL
Angular2 and Angular4 Development (Online)

08:00 AM - 10:00 AM IST(+5.30 GMT)

Know More
29 JUL
ASP.NET MVC with Angular4 Development (Classroom)

09:30 AM - 11:00 AM IST (+5:30GMT)

Know More
29 JUL
Xamarin Forms : Build Cross-platform Apps (Classroom)

09:00 AM - 11:00 AM IST(+5:30 GMT)

Know More
26 JUL
ASP.NET MVC with Angular4 Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

Know More
26 JUL
ASP.NET MVC with Angular4 Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

22 JUL
MEAN Stack 2 Development (Classroom)

04:00 PM - 05:30 PM IST(+5:30GMT)

20 JUL
Hadoop Development cum Administration (Online)

07:00 AM - 09:00 AM IST(+5:30 GMT)

17 JUL
NodeJS with Angular4 Development (Online)

08:00 PM - 09:30 PM IST(+5.30 GMT)

15 JUL
ASP.NET MVC with Angular4 Development (Online)

03:30 PM - 05:30 PM IST(+5.30 GMT)

1 JUL
ASP.NET MVC with Angular4 Development (Classroom)

08:00 AM - 09:30 AM IST

1 JUL
MEAN Stack 2 Development (Classroom)

11:00 AM - 12:30 PM IST(+5:30 GMT)

30 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

09:30 PM - 11:00PM IST(+5:30GMT)

6 JUN
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

20 MAY
NodeJS with Angular4 Development (Classroom)

05:00 PM - 07:00 PM

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+