Facade Pattern

Facade Pattern

Facade pattern is very common in object-oriented programming has been used widely. Façade name itself is through analogy is an architectural facade. By definition facade pattern is object which provides simplified interface client to larger body of programing for example java API class library. Below are some points which façade can do:

  • Because of common task and convenient method it makes software library easy to use, understand and perform test.
  • It also makes class library more readable with same explanation.
  • It reduces dependencies between outside coding and inner workings of library because if most code uses facade pattern means it allows more flexibility to developing the system.
  • It wraps up poorly designed collection of APIs with single good designed API according to need of the tasks.

Below is example will show you implementation of façade pattern using ordering pizza? Whenever you go to pizza restaurant there you call waiter give your choice of pizza order and wait for it. Basically here waiter is acting as facade which takes your order and use helper class to prepare pizza and finally deliver your order and you are unaware of process of making pizza.

  • Create abstract class Pizza:
public abstract class Pizza {
	abstract String prepareDough();
	abstract String addSauce();
	abstract String addCheese();
	abstract String addToppings();
}

 

  • Create concrete class NonVegetarianPizza:
public class NonVegetarianPizza extends Pizza {

	public String prepareDough(){
		return "Soft";		
	}
	public String addSauce(){
		return "tomato Sauce";		
	}
	public String addCheese(){
		return "Mozzarella";		
	}
	public String addToppings(){
		return "Onions, Ham, Peppers, Mushrooms";		
	}
}

 

  • Create concrete class VegetarianPizza:
public class VegetarianPizza extends Pizza{

	public String prepareDough(){
		return "Soft";		
	}
	public String addSauce(){
		return "tomato Sauce";		
	}
	public String addCheese(){
		return "Mozzarella";		
	}
	public String addToppings(){
		return "Onions, Spinach Mushrooms";		
	}
}

 

  • Create pojo class which will be use to store pizza order details:
public class PizzaOrdered {

	private String dough;
	private String sauce;
	private String cheese;
	private String toppings;
        //Get dough
	public String getDough() {
		return dough;
	}
        //Set Dough
	public void setDough(String dough) {
		this.dough = dough;
	}
        //Get sauce
	public String getSauce() {
		return sauce;
	}
        //Set sauce
	public void setSauce(String sauce) {
		this.sauce = sauce;
	}
        //Get cheese
	public String getCheese() {
		return cheese;
	}
        //Set cheese
	public void setCheese(String cheese) {
		this.cheese = cheese;
	}
	public String getToppings() {
		return toppings;
	}
	public void setToppings(String toppings) {
		this.toppings = toppings;
	}

}

 

  • Create Facade class Waiter:
public class Waiter {

	public PizzaOrdered constructPizza(Pizza pizza) {

		PizzaOrdered pizzaOrdered=new PizzaOrdered();					
		pizzaOrdered.setDough(pizza.prepareDough());
		pizzaOrdered.setSauce(pizza.addSauce());
		pizzaOrdered.setCheese(pizza.addCheese());			
		pizzaOrdered.setToppings(pizza.addToppings());

		return pizzaOrdered;	

	}

}

 

  • Finally create Customer class to place an Order:
public class CustomerOrderForPizza {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Waiter waiter=new Waiter();
		//Order for Non-Vegetarian Pizza
		NonVegetarianPizza nonVegetarianPizza=new NonVegetarianPizza();
		PizzaOrdered pizzaOrdered=waiter.constructPizza(nonVegetarianPizza);
		System.out.println("Non-Vegetarian Pizza Order details:\n"+"Dough Type: "+pizzaOrdered.getDough()+
				"\nSauce Type: "+pizzaOrdered.getSauce()+"\nCheese Type: "+pizzaOrdered.getCheese()+
				"\nToppings: "+pizzaOrdered.getToppings());

		//Order for Vegetarian Pizza
		VegetarianPizza vegetarianPizza=new VegetarianPizza();
		PizzaOrdered pizzaOrdered2=waiter.constructPizza(vegetarianPizza);
		System.out.println("\nVegetarianPizza Order details:\n"+"Dough Type: "+pizzaOrdered2.getDough()+
				"\nSauce Type: "+pizzaOrdered2.getSauce()+"\nCheese Type: "+pizzaOrdered2.getCheese()+
				"\nToppings: "+pizzaOrdered2.getToppings());		

	}

}

 

  • That’s it. Below is list of all design patterns link:

Creational Patterns:

Structural Patterns:

Behavioral Patterns:

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *