Introduction
The Façade Design Pattern provides a simple interface and controls access to a series of complicated interfaces and or sub systems.
Some of the important features and advantages of Façade design Pattern are:
- A façade can make a software library easier to use and understand, since the façade has convenient methods for common tasks
- A façade can make code that uses the library more readable, for the same reason
- A façade can reduce dependencies of outside code on the inner workings of a library, since most code uses the façade , thus allowing more flexibility in developing the system
- A façade can wrap a poorly-designed collection of APIs with a single well-designed API
In the following example, a developer using the OrderFacade
does not need to understand all the requirements of creating a proper Order
class. These details are hidden away behind the façade.
A VB and C# example of the Façade Pattern
Dim anOrderFacade As New OrderFacade
Dim aOrderID As Integer = anOrderFacade.placeOrder(234324324, myBasketItems)
Public Class OrderFacade
Public Function placeOrder(ByVal CustomerID As Integer, ByVal products As List(Of basketItems)) As Integer
Dim anOrder As New Order
Dim anOrderLine As New OrderLine
Dim despatchAddress As Address = Address.getCustomerDespatchAddress(CustomerID)
Dim orderID As Integer = anOrder.requestOrderID
anOrder.createOrder(orderID, despatchAddress)
anOrderLine.addOrderLinesToOrder(orderID, products)
Return orderID
End Function
End Class
Public Class Order
Public Function requestOrderID() As Integer
End Function
Public Sub createOrder(ByVal OrderID As Integer, ByVal despatchAddress As Address)
End Sub
End Class
Public Class OrderLine
Public Sub addOrderLinesToOrder(ByVal OrderID As Integer, ByVal Products As List(Of basketItems))
End Sub
End Class
Public Class Address
Public Shared Function getCustomerDespatchAddress(ByVal CustomerID As Integer) As Address
End Function
End Class
using System;
using System.Collections.Generic;
namespace Yanesh.DesignPatterns.Facade
{
public class OrderFacade
{
public int placeOrder(int CustomerID, List<BasketItem> Products)
{
Order anOrder = new Order();
OrderLine anOrderLine = new OrderLine();
Address DespatchAddress = Address.getCustomerDespatchAddress(CustomerID);
int OrderId = anOrder.requestOrderID();
anOrder.createOrder(OrderId, DespatchAddress);
anOrderLine.addOrderLinesToOrder(OrderId, Products);
return OrderId;
}
}
public class Order
{
public int requestOrderID()
{
}
public void createOrder(int OrderId, Address DespatchAddress)
{
}
}
public class OrderLine
{
public void addOrderLinesToOrder(int OrderId, List<BasketItem> Products)
{
}
}
public class Address
{
public static Address getCustomerDespatchAddress(int CustomerID)
{
return new Address();
}
}
public class BasketItem
{
}
}
UML Diagram
The Façade however, can itself become too complex for a huge subsystem. Also it's a good idea to actually have an abstract Façade over the Façade.
One of the most common and successful examples is using this pattern through a web service, making the web service acting as the Façade or the interface to many different Dll's each representing a subsystem.
Articles