标签:生成器模式 builder 设计模式 design pattern
生成器是一种对象创建型的模式。生成器将一个复杂对象的构建和它的表示分离,使得同样的构建过程可以创建不同的表示。
Builder: 为创建一个Product对象的各个部件指定抽象接口。(工厂方法是为整个对象)
ConcreteBuilder: 实现Builder的接口以构造和装配该产品的各个部件;定义并明确它所创建的表示;提供一个检索产品的接口。
Director: 构造一个使用Builder接口的对象。
Product: 表示被构造的复杂对象,ConcreteBuilder创建该产品的内部表示并定义它的装配过程。
如图所示,Order类为导向器(Director),用来产生一个Builder的对象。MealBuilder就是抽象Builder,它的两个子类是AmericanMealBuilder和ChineseMealBuilder是具体Builder用于一步一步地构造meal的各个部分。Meal类是Product。TestMeal是测试类。
下面上源码:
Order.java:
package com.andy.designpattern.builder; public class Order { private MealBuilder mealBuilder; public void makeOrder(MealBuilder mb){ mealBuilder = mb; } public void createMeal(){ mealBuilder.createMeal(); mealBuilder.buildMainFood(); mealBuilder.buildDrink(); mealBuilder.buildDish(); mealBuilder.buildSauce(); } public Meal getMeal(){ return mealBuilder.getMeal(); } }
package com.andy.designpattern.builder; public abstract class MealBuilder { protected Meal meal; // let child class see it public Meal getMeal(){ return this.meal; } public void createMeal(){ meal = new Meal(); } public abstract void buildMainFood(); public abstract void buildDrink(); public abstract void buildDish(); public abstract void buildSauce(); }
package com.andy.designpattern.builder; public class AmericanMealBuilder extends MealBuilder { public void buildMainFood() { // TODO Auto-generated method stub this.meal.setMainFood("fries"); } public void buildDrink() { // TODO Auto-generated method stub this.meal.setDrink("coke"); } public void buildDish() { // TODO Auto-generated method stub this.meal.setDish("hamburger"); } public void buildSauce() { // TODO Auto-generated method stub this.meal.setSause("ketchup"); } }
package com.andy.designpattern.builder; public class ChineseMealBuilder extends MealBuilder { public void buildMainFood() { // TODO Auto-generated method stub this.meal.setMainFood("rice,noodles"); } public void buildDrink() { // TODO Auto-generated method stub this.meal.setDrink("soup"); } public void buildDish() { // TODO Auto-generated method stub this.meal.setDish("vegetables and meat"); } public void buildSauce() { // TODO Auto-generated method stub this.meal.setSause("peanut sause"); } }
package com.andy.designpattern.builder; public class Meal { private String mainFood; private String drink; private String dish; private String sause; public String getMainFood() { return mainFood; } public void setMainFood(String mainFood) { this.mainFood = mainFood; } public String getDrink() { return drink; } public void setDrink(String drink) { this.drink = drink; } public String getDish() { return dish; } public void setDish(String dish) { this.dish = dish; } public String getSause() { return sause; } public void setSause(String sause) { this.sause = sause; } public String toString(){ return "MainFood:"+mainFood+"\nDrink:"+drink+"\nDish:"+dish+"\nSause:"+sause+"\n"; } }TestMeal.java:
package com.andy.designpattern.builder; public class TestMeal { public static void main(String[] args) { MealBuilder chineseBuilder = new ChineseMealBuilder(); MealBuilder americanBuilder = new AmericanMealBuilder(); Order order = new Order(); order.makeOrder(chineseBuilder); order.createMeal(); Meal meal1 = order.getMeal(); order.makeOrder(americanBuilder); order.createMeal(); Meal meal2 = order.getMeal(); System.out.println("Chinese :\n"+meal1.toString()); System.out.println("American :\n"+meal2.toString()); } }
Chinese : MainFood:rice,noodles Drink:soup Dish:vegetables and meat Sause:peanut sause American : MainFood:fries Drink:coke Dish:hamburger Sause:ketchup
抽象工厂模式与生成器模式相似,都可以用于创建复杂的对象。主要区别是生成器模式是一步步地构造对象,产品在最后一步才返回,而抽象工厂是生产一系列(常常是多个)产品,并且立即返回产品。
参考资料:
2.Tutorialspoint: Builder Pattern
标签:生成器模式 builder 设计模式 design pattern
原文地址:http://blog.csdn.net/lkasdolka/article/details/42294869