目标:
将复杂对象的创建过程和对象的表现分离,从而相同的创建步骤可以生成不同表现的对象。
静态类图:
Talk Is Simple:
Demo: 快餐店儿童套餐,套餐主要包含一个主食、一个甜点、一份饮料、一个小玩具。假设,目前快餐店针对小男孩和小女孩推出了两套不同的套餐!利用Builder设计模式实现如下:
/**
儿童套餐
**/
public class ChildrenMeal{
private String mainItem;
private String sideItem;
private String drink;
private String toy;
/**
sets 方法
**/
}
/**
抽象构造者
**/
public abstract class MealBuilder{
private ChildrenMeal childrenMeal;
public ChildrenMeal getMeal(){return this.childrenMeal;}
public abstract void prepareMainItem();
public abstract void prepareSideItem();
public abstract void prepareDrink();
public abstract void prepareToy();
}
public class BoyMealBuilder extends MealBuilder{
public BoyMealCashier(){
this.childrenMeal = new ChildrenMeal();
}
public void prepareMainItem(){
System.out.println("男娃套餐主食是鸡腿堡");
}
public void prepareSideItem(){
System.out.println("男娃套餐甜点是薯条");
}
public void prepareDrink(){
System.out.println("男娃套餐饮料是可乐");
}
public void prepareToy(){
System.out.println("男娃玩具是恐龙玩具");
}
}
public class GirlMealBuilder{
public BoyMealCashier(){
this.childrenMeal = new ChildrenMeal();
}
public void prepareMainItem(){
System.out.println("女娃套餐主食是蔬菜堡");
}
public void prepareSideItem(){
System.out.println("女娃套餐甜点是甜甜圈");
}
public void prepareDrink(){
System.out.println("女娃套餐饮料是杯橙汁");
}
public void prepareToy(){
System.out.println("女娃套餐玩具是芭比娃娃");
}
}
/*Director */
public class Cashier{
private MealBuilder builder;
public void setBuilder(MealBuilder builder){
this.builder = builder;
}
public ChildrenMeal getChildrenMeal(){
builder.prepareMainItem();
builder.prepareSideItem();
builder.prepareDrink();
builder.preapreToy();
}
}
public class Main{
public static void main(String args[]){
Cashier cashier = new Cashier();
cashier.setBuilder(new BoyMealBuilder());
ChildrenMeal meal = cashier.getChildrenMeal();
}
}
变形:
如上模式的Builder模式中,产品的构建步骤的顺序以及每个步骤的实现方式分别在Director 和具体的Builder类限定了,当某一天小女汉子非要恐龙不喜欢芭比娃娃,要吃鸡腿堡,需要为个性化的需求提供一个专用的Builder实现类,为支持客户端的灵活性,需要改造!
public abstract class MealBuilder{
private ChildrenMeal meal;
public ChildrenMeal getMeal(){return this.meal;}
public abstract Builder prepareMainItem(String msg);
public abstract Builder prepareSideItem(String msg);
public abstract Builder prepareDrink(String msg);
public abstract Builder prepareToy(String msg);
}
public class ConcreteMealBuilder extends MealBuilder{
public ConcreteMealBuilder(){
this.meal = new ChildrenMeal();
}
public Builder prepareMainItem(String msg){
this.meal.setMainItem(msg);
return this;
}
public Builder prepareSideItem(String msg){
this.meal.setSideItem(msg);
return this;
}
public Builder prepareDrink(String msg){
this.meal.setDrink(msg);
return this;
}
public Builder prepareToy(String msg){
this.meal.setToy(msg);
return this;
}
}
/**
儿童套餐
**/
public class ChildrenMeal{
private String mainItem = "鸡腿堡";
private String sideItem = "甜甜圈";
private String drink = "橙汁";
private String toy ="芭比娃娃";
/**
sets 方法
**/
}
public class Children{
public static void main(String args[]){
ConcreateBuilder cashier = new ConcreateBuilder();
cashier.setMainItem("鸡腿堡").setToy("恐龙");
}
}