Facade Design Pattern

The Facade Design Pattern provides a simple unified interface of a complex system to a client to abstract the complexity and make it easier for client to use. Facade defines a single high level interface that makes the subsystem easier to use. Facade pattern comes under structural pattern as it provides one of the best ways to hide internal complexities from client.

A facade is a composition of subsystems and uses delegates the tasks to subsystems. It may also add some logic of it's own before delegating work to subsystem. However, it doesn't hide the subsystem interfaces form the client. A client is free to directly interact with subsystem, it is on client whether to use facade or not.

Advantages of Facade Pattern

  • A facade hides the complexity of subsystem and provides a simple interface to client.
  • It decouples the client implementation from the subsystems. We can any of the subsystem without modifying client's code provides client is only interacting through facade.

When we should use Facade Pattern

  • When we want to provide single unified interface to client to interact with a complex subsystem.
  • When we want to hide the internal complexities of system.
  • When we want to decouple the client's implementation form subsystems.

Implementation of Facade Design Pattern

First of all we will define the subsystems of a order fulfillment systems.

ReserveInventory.java

This class reserves the item for this order.

public class ReserveInventory {
    public void reserveInventory(){
        System.out.println("Blocking Item for Customer..");
    }
}

ReservePayment.java

This class deducts the order amount form customers credit card.

public class ReservePayment {
   public void receivePayment(){
      System.out.println("Deducting amount from customer's credit card");
   }
}

DeliverySystem.java

This class manages the delivery of an item to customers address.

public class DeliverySystem {
   public void deliverOrder(){
      System.out.println("Deliverying Item to customer's address");
   }
}

CancelOrder.java

This class cancels customers order.

public class CancelOrder {
    public void cancelOrder(){
        System.out.println("Cancelling order");
    }
}

RefundPayment.java

This class refunds the order amount to customer on order cancellation.

public class RefundPayment {
   public void refundOrderAmount(){
      System.out.println("Refunding Order Amount to Customer");
   }
}

Now, to place an order we have to first reserve inventory and the reserve payment(deduct customers credit's card) and finally deliver the item to customer. To hide the complexity of placing order from a third party client we will provide a simple method "placeOrder" in OrderManagementFacade which will handle every thing required for placing an order. Similarly, we will provide "cancelOrder" method in facade for cancelling an order. Order Management Facade hides the complexities of fulfillment subsystems form client.

OrderManagementFacade.java
package FacadePattern;

public class OrderManagementFacade {
    ReserveInventory reserveInventory;
    ReservePayment reservepayment;
    RefundPayment refundPayment;
    DeliverySystem deliverySystem;
    CancelOrder cancelOrder;
 
    public OrderManagementFacade(){
        reserveInventory = new ReserveInventory();
        reservepayment = new ReservePayment();
        refundPayment = new RefundPayment();
        deliverySystem = new DeliverySystem();
        cancelOrder = new CancelOrder();
    }
 
    public void placeOrder(){
        reserveInventory.reserveInventory();
        reservepayment.receivePayment();
        deliverySystem.deliverOrder();
    }
 
    public void cancelOrder(){
 cancelOrder.cancelOrder();
        refundPayment.refundOrderAmount();
    }
}

We will define a client class FacadePatternExample.java which will place and cancel the order using Order Management Facade.

FacadePatternExample.java
public class FacadePatternExample {
    public static void main(String args[]){
 OrderManagementFacade orderingSystem = new OrderManagementFacade();
  
 // Place Order 
 orderingSystem.placeOrder();
 System.out.println("--------------------");
 // Cancel Order 
 orderingSystem.cancelOrder();
    }
}

Output

Blocking Item for Customer..
Deducting amount from customer's credit card ..
Deliverying Item to customer's address ..
--------------------
Cancelling order ..
Refunding Order Amount to Customer..

Related Topics
Bridge Design Pattern
Mediator Design Pattern
Interpreter Design Pattern
Factory Design Pattern
Prototype Design Pattern
Singleton Design Pattern
Builder Design Pattern
Memento Design Pattern
Observer Design Pattern
State Design Pattern
List of Design Patterns