Wednesday, 3 September 2014

Facade Design Pattern Implementation

Facade Design Pattern

This design pattern falls under structural pattern. As we know the meaning of Facade is the face or front of a building. This pattern helps to hide the complexities of the system and provides an interface to the client. It helps to design a wrapper class or a higher level interface and making subsystems easier to use because it is exposing a simplified interface for the client. It also helps for achieving abstraction and for encouraging loose coupling. For more description refer the below link:

http://www.javatechtipssharedbygaurav.com/2014/03/java-design-patterns-part-2.html

Example :- In any eCommerce website, we can see that order processing screen is working as a facade between Stock and Payment. Now we will implement the same concept in programming. 

User is placing the order without knowing of how internally it is processing. Once the order is being placed, then the façade layer calls the methods of the subsystems like Stock for item availablity check and Payment for processing of the payment. After processing, it returns the control to the user with a confirmation about the order being processed.

1. Stock.java

package com.gaurav.designpattern.facade;

/**
 * @author Gaurav
 * 
 */
public class Stock {
public boolean itemsAvailability(String productId) {
boolean availableFlag = false;
if ("U132897".equalsIgnoreCase(productId)) {
availableFlag = true;
} else if ("U987011".equalsIgnoreCase(productId)) {
availableFlag = true;
}
return availableFlag;
}
}

2. Payment.java

package com.gaurav.designpattern.facade;

/**
 * @author Gaurav
 * 
 */
import java.util.HashMap;

public class Payment {
public String getPayment(HashMap<String, String> cardDetailsMap) {
 boolean authFlag = true;
 String status = "Failed";
 if(authFlag){
  System.out.println("Payment is approved successfully - by Bank");
  status = "Success";
 }
 return status;
}
}

3. OrderFacadeInterface.java

package com.gaurav.designpattern.facade;

/**
 * @author Gaurav
 * 
 */
import java.util.HashMap;

public class OrderFacadeInterface {
private Stock stock = new Stock();
private Payment payment = new Payment();
public boolean placeOrder(HashMap<String, String> cardDetails, String productId){
boolean placeOrderStatus = false; 
boolean itemAvailability = stock.itemsAvailability(productId);
String paymentStatus = null;
if(itemAvailability){
System.out.println(productId+ " is available in the stock");
paymentStatus = payment.getPayment(cardDetails);
}
if("Success".equalsIgnoreCase(paymentStatus)){
placeOrderStatus = true;
}
return placeOrderStatus;
}
}

4. EcommerceMerchant.java

package com.gaurav.designpattern.facade;

/**
 * @author Gaurav
 * 
 */
import java.util.HashMap;

public class EcommerceMerchant {
public static void main(String args[]) {

String productId = "U987011";

HashMap<String, String> cardDetails = new HashMap<String, String>();
cardDetails.put("CardNumber", "2134567821345698");
cardDetails.put("CVV", "012");
cardDetails.put("ExpiryDate", "10/15");
OrderFacadeInterface ofi = new OrderFacadeInterface();
boolean orderStatus = ofi.placeOrder(cardDetails, productId);
System.out.println("Order is placed successfully : "+orderStatus);
}
}

Result :- 

U987011 is available in the stock
Payment is approved successfully - by Bank
Order is placed successfully : true

No comments:

Post a Comment