Software

Facade Pattern

Larger system => more complex system, which potentially confusing for the client classes in your system to use. System complexity is not always a sign for poor design, though.

The Facade design pattern provides a single simplified interface for client classes to interact with the subsystem. It doesn’t add any functionality, it simply acts as a point of entry into your subsystem. It’s wrapper class that encapsulates a subsystem in order to hide the complexity.

A Facade class can be used to wrap all the interfaces and classes for a subsystem.

Example how client code interacts without facade, the client/customer is responsible for properly instantiating each of these constituent classes and knows about all their different attributes and methods:

No Facade Class Diagram.png
Bank system without Facade pattern.

Instead, we can introduce the BankService class to act as a facade for checking, saving and investment classes (wrapping) and presenting a simpler front to them for the customer client class to use.

Facade Class Diagram.png
Bank system with the BankService Facade.

Code example

Step 1: Design the interface

public interface IAccount {
    public void deposit(decimal amount);
    public void withdraw(decimal amount);
    public void transfer(decimal amount);
    public int getAccountNumber();
}

Step 2: Implement the interface with one or more classes

public class Chequing: IAccount { ... }
public class Saving: IAccount { ... }
public class Investment: IAccount { ... }

Step 3: Create the facade class and wrap the classes that implement the interface

public class BankService{
    private Hashtable<int, IAccount> bankAccounts;

    public BankService(){
        this.bankAccounts = new Hashtable<int, IAccount>();
    }

    public int CreateNewAccount(string type, decimal initAmount){
        IAccount newAccount;

        switch(type) {
            case "chequing":
                newAccount = new Chequing(initAmount);
                break;
            case "saving":
                newAccount = new Saving(initAmount);
                break;
            case "investment":
                newAccount = new Investment(initAmount);
                break;
            default:
                Console.WriteLine("Invalid account type");
                break;
        }
        //The rest of the code 
    }

    public void TransferMoney(int to, int from, decimal amount){
        IAccount toAccount = this.bankAccounts.Get(to);
        //The rest of the code
    }
}

Step 4: Use the facade class to access the subsystem

public class Customer {
    public static void Main(String[] args){
        BankService myBankService = new BankService();

        int mySaving = myBankService.CreateNewAccount("saving", 500);

        int myInvestment = myBankService.CreateNewAccount("investment", 1000);

        myBankService.TransferMoney(mySaving, myInvestment, 300);
    }
}

Hence the customer simply needs to know about the BankService only, because of the complexity is hidden. While the facade design pattern uses a number of different design principles, it purpose is to provide ease of access to a complex subsystem. This is done by encapsulating the subsystem classes into a Facade class, and then hiding them from the client classes so that the clients do not know about the details of the subsystem.

One thought on “Facade Pattern

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.