Click here to Skip to main content
15,860,943 members
Articles / Programming Languages / Visual Basic

Facade

Rate me:
Please Sign up or sign in to vote.
4.55/5 (5 votes)
20 Oct 2013CPOL1 min read 13.5K   12   1
FacadeThe Facade Design Pattern provides a simple interface and controls access to a series of complicated interfaces and or sub systems.Some of

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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

VB.NET
' 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#
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 Diagram

Facade Design Pattern

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

This article was originally posted at http://wiki.asp.net/page.aspx/311/facade

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun28-May-14 21:49
Humayun Kabir Mamun28-May-14 21:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.