JB Header
Facade Design Pattern in Java
This tutorial explains Facade Design Pattern in Java with class diagrams and example code.
Introduction Facade Design Pattern is a structural design pattern among the Gang Of Four(GOF)Article on GOF Patterns & their types Design Patterns. Being a structural design pattern, the facade pattern basically deals with how the classes are composed together to get the desired outcome from a system. What is Facade Design Pattern The basic design problem which the facade pattern solves is to simplify access to a set of interfaces in a subsystem. Facade solves this problem by providing a higher-level interface one level above all the sub-level interfaces. This higher-level interface then deals with the details and intricacies of the subsystem interface and at the same time provides a simplified interfaces to the clients of the subsystem.

Facade pattern improves decoupling between the clients and subsystems as the higher-level interface can absorb any changes which might happen in the lower subsystems interfaces and provide a consistent interface to the clients. This also has a direct benefit of system portability as the clients only design and code for the higher-level interface defined by the Facade. Porting of the subsystems then becomes easy as all the complexity of porting is hidden from the clients by the Facade layer.

Layered subsystems can have a facade for each layer. Thus communication between layers of the subsystem is much simplified as it happens through the subsystem facades rather than directly deal with the complexities of the subsystem's individual classes.
Class Diagram for Facade Design Pattern
Facade Design Pattern Class Diagram
Explanation of Class Diagram
  • The classes to the extreme right belong the Sub-System(depicted by the UML notation of ::).
  • There are N subsystem classes-Class1,Class2...ClassN.
  • Facade layer, or Facade class as per this class diagram, invokes the sub-system classes as per the client request received by it on one of its simplifiedMethods().
  • SimplifiedMethods() in turn invoke multiple subsystem classes based on the business logic. SimplifiedMethods() may talk to multiple sub-system classes as per the business logic and in the end come up with the simplified/aggregated response which can be sent back to the client.
  • The Client class creates, or holds, an instance of the Facade and invokes the specific simplifiedMethod() as per his needs. He just invokes the simplifiedMethod() and in return gets the simplified/aggregate response. Thus, the Client is shielded from the underlying complexities of the sub-system layer by the Facade.
Example in Java - Class Diagram
Facade Design Pattern in Java - Class Diagram

Code for the classes shown in Java Class Diagram
//AccountDebitor.java
public class AccountDebitor{
  public void debitAccount(String accNo, Double amount){
   //Logic for debiting 'amount' from account number 'accNo'
  } 
}
//AccountCreditor.java
public class AccountCreditor{
  public void creditAccount(String accNo, Double amount){
   //Logic for crediting 'amount' into account number 'accNo'
  } 
}
//FDIssuer.java
public class FDIssuer{
  public void issueFD(Double amount){
   //Logic for issuing FD for 'amount'
  } 
}
//BankingFacade.java
public class BankingFacade{
  AccountCreditor creditor=new AccountCreditor();
  AccountDebitor debitor=new AccountDebitor();
  FDIssuer fdIssuer=new FDIssuer();
  public void onlineTransfer(String fromAcc, String toAcc, Double amount){
    debitor.debitAccount(fromAcc, amount);
    creditor.creditAccount(toAcc, amount);
  }
  public void atmWithdrawal(String fromAcc, Double amount){
    debitor.debitAccount(fromAcc, amount);
  } 
  public void issueFD(String fromAcc, Double amount){
    debitor.debitAccount(fromAcc, amount);
    fdIssuer.issueFD(amount);
  } 
}
//Client.java
public class Client{
  private BankingFacade facade;
  public Client(BankingFacade facade){
    this.facade=facade;
  }  
  public static void main(String args[]){
    BankingFacade facade=new BankingFacade();
    facade.onlineTransfer("1001","1002",10000.00);
    facade.atmWithdrawal("1001",1000.00);
    facade.issueFD("1002",500.00);
  } 
}
Explanation of Code & Class Diagram The Java class diagram above depicts facade pattern implemented for a banking system where -
  • AccountCreditor, AccountDebitor and FDIssuer are three classes in the banking sub-system which accomplish a functionality as represented by their name i.e. they credit an account with a specific amount, debit an account with the given amount, or issue an FD for a given amount respectively.
  • BankingFacade here holds handles to all the three sub-system classes. It invokes one or more of the sub-system classes based on the banking function requested by the Client.
  • For the operation online transfer on BankingFacade, Client invokes just one method onlineTransfer() with debit-account, credit-account and amount to be transferred. Internally BankingFacade invokes AccountDebitor to debit and then AccountCreditor to credit the amount. It also takes care of transactions and any exceptions which may happen.(I have not written code for transaction & exception handling in this example as it is not relevant to facade pattern as such).
  • Similar to onlineTransfer() method on BankingFacade, atmWithdrawal invokes a method of AccountDebitor. In the same way, issueFD() first invokes AccountDebitor to debit the amount of FD and then calls FDIssuer to issue the FD.
  • The important thing to note is that the Client remains unaware of the complexities of onlineTransfer(), atmWithdrawal() or issueFD() operations it invokes on BankingFacade. This is because the BankingFacade simplifies the underlying banking sub-system complexity by having simple input and output. But internally BankingFacade navigates through the sub-system complexity to make the banking operations work.
Summary In the above tutorial we looked at what is facade design pattern including what advantages it provides. We then looked at the class diagram for a facade design pattern & its explanation, class diagram for a Java example of Facade pattern & its explanation. This concludes the tutorial on facade design pattern.