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.

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

Facade_Pattern (1)

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

ReserveInventory.java

This class reserves the item for this order.

Facade Design Pattern

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.");
   }
}

Facade Design Pattern 1

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.

Facade Design Pattern 2

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.

Facade Design Pattern 3

OrderManagementFacade.java
package FacadePattern;
 
public class OrderManagementFacade {
    private ReserveInventory reserveInventory;
    private ReservePayment reservePayment;
    private RefundPayment refundPayment;
    private DeliverySystem deliverySystem;
    private 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.

Facade Design Pattern 4

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.