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.

Example Facade

In this section, we will review a simple example of the use of the facade design pattern. The wrapped code shown here is purely to demonstrate the pattern and should not be considered as a good design. Indeed, it is this type of poor design that can be a reason for hiding the implementation details behind a facade.

In this example, a web site needs to determine whether a particular product has reached a low stock level. The business logic of the underlying application is difficult to use for this purpose, requiring calls to multiple methods and properties and the use of a connection to a SQL Server database that is irrelevant to the operation of the web site.

The following code shows the elements that are required to obtain the required information:

public class Product
{
    private SqlConnection _connection;
    private string _itemNumber;

    public Product(string itemNumber, SqlConnection connection)
    {
        _connection = connection;
        _itemNumber = itemNumber;
    }

    public int PhysicalStock
    {
        get
        {
            // Retrieve stock level from database.
        }
    }

    public int StockOnOrder
    {
        get
        {
            // Retrieve incoming ordered stock from database.
        }
    }

    public int LowStockLevel
    {
        get
        {
            // Retrieve low stock level from database.
        }
    }
}


public static class StockAllocator
{
    public static int GetAllocations(string itemNumber, SqlConnection connection)
    {
        // Retrieve allocated stock for product from database.
    }
}

There are two classes in operation in this fragment of the business application. The first class, named "Product", represents a single product stocked by the company. This class is instantiated with two parameters; the item code for the product and the details of a SQL Server database connection to use to obtain the product information. The second class is a static class used to allocate and deallocate stock. The method that interests us is used to return the number of units that are currently allocated.

NB: The implementation of each method is irrelevant so has been omitted to keep the sample as short as possible. A .NET 2.0 static class is included in the example but could be declared as a standard class for .NET 1.1 use.

In order to determine if a product has reached a limited stock level, the following actions are required:

  • Obtain a connection to the SQL Server database.
  • Create an instance of the Product class, passing the connection and the item number of the product to query.
  • Retrieve the current physical stock level for the item using the PhysicalStock property of the product.
  • Retrieve the number of items that have been ordered from suppliers and that will be added to the physical stock level shortly, using the StockOnOrder property.
  • Obtain the low stock level for the product using the LowStockLevel property.
  • Determine the currently allocated stock for the product using the StockAllocator class.
  • Calculate the current stock and compare it to the low stock level.

This implement is rather complicated and shows the implementation details for the business application. To simplify the process for use by the web site, we can add all of these steps to a single method within a facade class. This facade is shown below. Some elements are simplified to shorten the sample.

public class StockFacade
{
    public bool IsLowStock(string itemNumber)
    {
        SqlConnection conn = GetConnection();

        Product product = new Product(itemNumber, conn);

        int physical = product.PhysicalStock;
        int onOrder = product.StockOnOrder;
        int lowStock = product.LowStockLevel;
        int allocations = StockAllocator.GetAllocations(itemNumber, conn);

        int available = physical + onOrder - allocations;
        return (available <= lowStock);
    }
}

Testing the Facade

We can test the example facade code using a console application. In the following Main method, the facade is instantiated and a single method is called. The result of the method is a Boolean value indicating whether if the product has reached limited stock. This code shows how much simpler the use of the business logic is when accessed via a facade.

static void Main(string[] args)
{
    StockFacade facade = new StockFacade();
    bool low = facade.IsLowStock("ABC123");
}
19 October 2008