BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

Facade Design Pattern

The facade pattern is a design pattern that is used to simplify access to functionality in complex or poorly designed subsystems. The facade class provides a simple, single-class interface that hides the implementation details of the underlying code.

What is the Facade Pattern?

The facade pattern is a Gang of Four design pattern. This is a structural pattern as it defines a manner for creating relationships between classes or entities. The facade design pattern is used to define a simplified interface to a more complex subsystem.

The facade pattern is ideal when working with a large number of interdependent classes, or with classes that require the use of multiple methods, particularly when they are complicated to use or difficult to understand. The facade class is a "wrapper" that contains a set of members that are easily understood and simple to use. These members access the subsystem on behalf of the facade user, hiding the implementation details.

The facade design pattern is particularly useful when wrapping subsystems that are poorly designed but cannot be refactored because the source code is unavailable or the existing interface is widely used. Sometimes you may decide to implement more than one facade to provide subsets of functionality for different purposes.

One example use of the facade pattern is for integrating a web site with a business application. The existing software may include large amounts of business logic that must be accessed in a particular manner. The web site may require only limited access to this business logic. For example, the web site may need to show whether an item for sale has reached a limited level of stock. The IsLowStock method of the facade class could return a Boolean value to indicate this. Behind the scenes, this method could be hiding the complexities of processing the current physical stock, incoming stock, allocated items and the low stock level for each item.

Implementing the Facade Pattern

Facade Design Pattern UML

The UML class diagram above describes an implementation of the facade design pattern. The items in the diagram are described below:

  • Facade. This class contains the set of simple functions that are made available to its users and that hide the complexities of the difficult-to-use subsystems.
  • Package (A/B). The complex functionality that is accessed via the facade class does not necessary reside in a single assembly. The packages in the diagram illustrate this, as each can be an assembly containing classes.
  • Class (A/B,1/2). These classes contain the functionality that is being presented via the facade.

The following shows the basic code of the facade design pattern implemented using C#. For simplicity, all of the classes involved are defined in a single code listing. In reality, these would probably be in different assemblies. Each class includes a sample method that is called from within the single facade method, with the results being passed to other methods from the subsystem.

public class Facade
{
    public void PerformAction()
    {
        Class1A c1a = new Class1A();
        Class1B c1b = new Class1B();
        Class2A c2a = new Class2A();
        Class2B c2b = new Class2B();

        int result1a = c1a.Method1A();
        int result1b = c1b.Method1B(result1a);
        int result2a = c2a.Method2A(result1a);
        c2b.Method2B(result1b, result2a);
    }
}


public class Class1A
{
    public int Method1A()
    {
        // Sample code
    }
}


public class Class1B
{
    public int Method1B(int param)
    {
        // Sample code
    }
}


public class Class2A
{
    public int Method2A(int param)
    {
        // Sample code
    }
}


public class Class2B
{
    public void Method2B(int param1, int param2)
    {
        // Sample code
    }
}
19 October 2008