Facade

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

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +