Design Pattern - Facade

Facades as the name suggests means the face of the building ,Operating System are one such example, You don't see all the inner workings of your computer, but the OS provides a simplified interface to use the machine, buildings also have a facade-the exterior of the building.
Facades design pattern hides the complexities of the system and provide one interface to the client for where the client can access the system, In Java JDBC can be called facade, we a users or clients create connection using the “java.sql.Connection” interface, the implementation of which we are not concerned about. The implementation is left to the vender of drive. 
Following sequence diagram illustrates how the pattern is used by a client:



Facade makes the API and libraries to use which is good for maintenance and readability .It can also collate and abstract various poorly designed APIs with a single simplified API.
Code Example:

ShippingGroup.java
public class ShippingGroup{
  public String checkShippingGroup(String OrderId) {
       return 'ShippingGroup checked';
   }
 }
PaymentGroup.java
public class PaymentGroup {
   public String deductPaymentGroup(String orderID) {
       return 'Payment deducted successfully';  
   }
}
OrderManager.java
public class OrderManager{
   private PaymentGroup pymt = new PaymentGroup();
   private ShippingGroup ship = new ShippingGroup();
 
   public void placeOrder(String orderId) {
       String step1 = ship.checkShippingGroup(orderId);
       String step2 = pymt.checkPaymentGroup(orderId);
       System.out.println('Following steps completed:' + step1  + ' & ' + step2);
   }
}
Client.java
public class Client {
  public static void main(String args[]){
    OrderManager ordermanager = new OrderManager();
    OrderManager.placeOrder('OR123456');
    System.out.println('Order processing completed');
  }
}

No comments: