Facade
Imagine, if you will, that the client had to utilize each
individual 'course' classes (Soup, Salad, MainCourse, Dessert)
and access a data base to build a dinner.
The facade class manages all the 'course' classes and
data base access for the client.
This gives the client only one class (the facade)
it must interact with when building a dinner.
The facade class.
-----------------------------------------------------
class DinnerFacade {
private Dessert dessert = new Dessert();
private Soup soup = new Soup();
private Salad salad = new Salad();
private MainCourse mainCourse = new MainCourse();
public void setDessert(String name){
dessert.setName(name);
dessert.setRecipe(this.lookUpRecipe(name));
}
public void setSoup(String name){
soup.setName(name);
soup.setRecipe(this.lookUpRecipe(name));
}
public void setSalad(String name){
salad.setName(name);
salad.setRecipe(this.lookUpRecipe(name));
}
public void setMainCourse(String name){
mainCourse.setName(name);
mainCourse.setRecipe(this.lookUpRecipe(name));
}
public String getDinnerMenu(){
String returnString ="\n";
returnString += soup.getName()+"\n";
returnString += salad.getName()+"\n";
returnString += mainCourse.getName()+"\n";
returnString += dessert.getName()+"\n";
return returnString;
}
public String getDinnerRecipes(){
String returnString ="\n";
returnString += soup.getName()+"\n";
returnString += soup.getRecipe()+"\n\n";
returnString += salad.getName()+"\n";
returnString += salad.getRecipe()+"\n\n";
returnString += mainCourse.getName()+"\n";
returnString += mainCourse.getRecipe()+"\n\n";
returnString += dessert.getName()+"\n";
returnString += dessert.getRecipe()+"\n\n";
return returnString;
}
private String lookUpRecipe(String name){
//get recipe from data base?
String returnString ="a pretend recipe\n";
returnString += "do somthing\n";
for(int i =0;i<5;i++)
returnString += "do somthing else\n";
return returnString;
}
}
-----------------------------------------------------
The 'course' super class.
-----------------------------------------------------
class Course{
private String name;
private String recipe;
public void setName(String name){
this.name = name;
}
public void setRecipe(String recipe){
this.recipe = recipe;
}
public String getName(){
return name;
}
public String getRecipe(){
return recipe;
}
}
-----------------------------------------------------
soup, salad, mainCourse, dessert classes.
-----------------------------------------------------
class Soup extends Course {}
-----------------------------------------------------
class Salad extends Course {}
-----------------------------------------------------
class MainCourse extends Course {}
-----------------------------------------------------
class Dessert extends Course {}
-----------------------------------------------------
The client class.
-----------------------------------------------------
class Test{
public static void main(String[] args){
System.out.println("class: Test, method: main");
DinnerFacade dinnerFacade = new DinnerFacade();
dinnerFacade.setSoup("cream of mushroom");
dinnerFacade.setSalad("traditional greek salad");
dinnerFacade.setMainCourse("leg of lamb");
dinnerFacade.setDessert("tiara misu");
System.out.println(dinnerFacade.getDinnerMenu());
System.out.println(dinnerFacade.getDinnerRecipes());
}
}
-----------------------------------------------------
Here is a tar with the above java classes in it.
facade.tar