 .NET 1.1+Facade Design PatternThe 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

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
}
}
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");
}
|