Home / ASP.NET Wiki / Architecture / Design Patterns / Facade

Facade

 Rate It (8)

Facade

The Facade 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 Facade design Pattern are

  • A facade can make a software library easier to use and understand, since the facade has convenient methods for common tasks
  • A facade can make code that uses the library more readable, for the same reason
  • A facade can reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system
  • A facade 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 facade.

A VB example of the Facade Pattern

' Code within the application which uses the

' simple interface that the Order Facade exposes to

' place an order

Dim anOrderFacade As New OrderFacade

Dim aOrderID As Integer = anOrderFacade.placeOrder(234324324, myBasketItems)

 

' The Order Facade

Public Class OrderFacade

 

    ' Places an Order and Returns an Order ID

    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

 

' Order Class

Public Class Order

    Public Function requestOrderID() As Integer

        ' Creates and Returns a Unique Order ID

    End Function

 

    Public Sub createOrder(ByVal OrderID As Integer, ByVal despatchAddress As Address)

 

    End Sub

End Class

 

' OrderLine Class

Public Class OrderLine

    Public Sub addOrderLinesToOrder(ByVal OrderID As Integer, ByVal Products As List(Of basketItems))

 

    End Sub

End Class

 

' Public Customer

Public Class Address

    Public Shared Function getCustomerDespatchAddress(ByVal CustomerID As Integer) As Address

 

    End Function

 

    ' Address Properties...

 

End Class

 C# Example of above VB code

 

using System;
using System.Collections.Generic;


namespace Yanesh.DesignPatterns.Facade
{
public class OrderFacade
{
//Places an Order and Returns an Order ID
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;
}
}

//order class
public class Order
{
public int requestOrderID()
{
//Creates and Returns a Unique Order ID
}

public void createOrder(int OrderId, Address DespatchAddress)
{
}
}

//OrderLine Class
public class OrderLine
{
public void addOrderLinesToOrder(int OrderId, List<BasketItem> Products)
{
}
}

//Public Customer
public class Address
{
public static Address getCustomerDespatchAddress(int CustomerID)
{
return new Address();
}
//Address properties
}

public class BasketItem
{
//BasketItem Properties
}
}
 

UML

The Facade however, can itself become too complex for a huge subsystem. Also it's a good idea to actually have an abstract Facade over the Facade. One of the most common and successfull examples is using this pattern through a webservice, making the webservice acting as the Facade or the interface to many different dll's each representing a subsystem.

Facade Design Pattern

Articles

Revision number 11, Tuesday, September 28, 2010 6:07:15 PM by karan@dotnet

Comments

A facade can be useful in calling a webservice.The request ,response from a webservice and passing the response to other modules.

I happen to use this patter quite a lot and find it very useful in complex design.

Besides web service ,i found the best use in Exceptions handling for the libraries.

Where could I find the C# version of the Articles

@rajeshtheholy I have added a c# examle of the VB code.

thanks for posting this article

This type of pattern for which type of projects? Can anyone explain.

Related Articles

Design Patterns

Design Patterns Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers. Design patterns are used throughout the ASP.NET Framework. The various patterns are commonly divided into several different groups

Mediator Pattern

The mediator pattern encapsulate the interaction between a set of objects. Also it encapsulate the protocol between those objects. The pattern help to loosely couple the object by keeping them from referring one each other. Use Cases for the Mediator Pattern

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)