Monday, August 1, 2016

Facade Design Pattern

Facade pattern is one such pattern which provides a simplified interface to a set of interfaces within a system and thus it hides the complexities of the subsystem from the client.


 
In the above structure for Facade pattern the Facade class insulates the subsystem from the client. The client only interacts with the Facade class without knowing about the subsystem classes.

Example:
Let’s take an example of Order processing online website. The client has placed an order without having knowledge of how internal classes are functioning. Once the order is placed the facade class layer calls the methods of the subsystems like Inventory for inventory check and Payment for processing of the payment. After processing it returns the control to the client class with the confirmation about the order being processed.
Sequence Diagram:
 

Code Example:

Inventory.java
public class Inventory {
    public String checkInventory(String OrderId) {
        return "Inventory checked";
    }
}

Payment.java
public class Payment {
    public String deductPayment(String orderID) {
        return "Payment deducted successfully";

    }
}

OrderFacade.java
public class OrderFacade {
    private Payment pymt = new Payment();
    private Inventory inventry = new Inventory();

    public void placeOrder(String orderId) {
        String step1 = inventry.checkInventory(orderId);
        String step2 = pymt.deductPayment(orderId);
        System.out
                .println("Following steps completed:" + step1
                        + " & " + step2);
    }
}

Client.java
public class Client {
  public static void main(String args[]){
      OrderFacade orderFacade = new OrderFacade();
      orderFacade.placeOrder("OR123456");
      System.out.println("Order processing completed");
  }
}

Advantages of Facade Pattern
Facade pattern allows loose coupling between client and subsystems.
It defines a high level interface that makes using the subsystems easier.

Difference between Adapter and Facade pattern
Adapter pattern alters an interface so that it matches what the client is expecting.
The aim of Facade pattern is to provide a simplified interface to a subsystem.

Facade pattern uses
Facade pattern is commonly used in following scenarios:
– To provide interface to legacy back-end system
– To reduce network calls: Facade makes many calls to subsystems, but remote client makes only one call to facade
– To encapsulate flow and inner details for data security

Drawbacks/Consequences:
  • One of the drawback is that the subsystem methods are connected to the Facade layer. If the structure of the subsystem changes then it will require subsequent change to the Facade layer and client methods.
Interesting points:
Facade pattern might be confused with mediator pattern. Mediator also abstracts the functionality of the subsystem in similar manner to facade. However, there is a subtle difference between both these patterns. In case of Mediator pattern the subsystem is aware of the mediator, however in case of Facade the subsystem does not know anything about the facade. It’s a one-way communication from Facade to subsystem.

Facade used in Java API
javax.servlet.http.HttpSession
javax.servlet.http.HttpServletRequest
javax.servlet.http.HttpServletResponse
javax.faces.context.ExternalContext

No comments:

Post a Comment