人在江湖

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks
Builder

Abstract interface for creating objects (product).

Concrete Builder

Provide implementation for Builder. Construct and assemble parts to build the objects.

Director

The Director class is responsible for managing the correct sequence of object creation. It receives a Concrete Builder as a parameter and executes the necessary operations on it.

Product

The final object that will be created by the Director using Builder..

   1: /** "Product" */
   2: class Pizza {
   3: private String dough = "";
   4: private String sauce = "";
   5: private String topping = "";
   6:  
   7: public void setDough(String dough) {
   8: this.dough = dough;
   9: }
  10:  
  11: public void setSauce(String sauce) {
  12: this.sauce = sauce;
  13: }
  14:  
  15: public void setTopping(String topping) {
  16: this.topping = topping;
  17: }
  18: }
  19:  
  20: /** "Abstract Builder" */
  21: abstract class PizzaBuilder {
  22: protected Pizza pizza;
  23:  
  24: public Pizza getPizza() {
  25: return pizza;
  26: }
  27:  
  28: public void createNewPizzaProduct() {
  29: pizza = new Pizza();
  30: }
  31:  
  32: public abstract void buildDough();
  33:  
  34: public abstract void buildSauce();
  35:  
  36: public abstract void buildTopping();
  37: }
  38: /** "ConcreteBuilder" */
  39: class HawaiianPizzaBuilder extends PizzaBuilder {
  40: public void buildDough() {
  41: pizza.setDough("cross");
  42: }
  43:  
  44: public void buildSauce() {
  45: pizza.setSauce("mild");
  46: }
  47:  
  48: public void buildTopping() {
  49: pizza.setTopping("ham+pineapple");
  50: }
  51: }
  52:  
  53: /** "ConcreteBuilder" */
  54: class SpicyPizzaBuilder extends PizzaBuilder {
  55: public void buildDough() {
  56: pizza.setDough("pan baked");
  57: }
  58:  
  59: public void buildSauce() {
  60: pizza.setSauce("hot");
  61: }
  62:  
  63: public void buildTopping() {
  64: pizza.setTopping("pepperoni+salami");
  65: }
  66: }
  67: /** "Director" */
  68: class Cook {
  69: private PizzaBuilder pizzaBuilder;
  70:  
  71: public void setPizzaBuilder(PizzaBuilder pb) {
  72: pizzaBuilder = pb;
  73: }
  74:  
  75: public Pizza getPizza() {
  76: return pizzaBuilder.getPizza();
  77: }
  78:  
  79: public void constructPizza() {
  80: pizzaBuilder.createNewPizzaProduct();
  81: pizzaBuilder.buildDough();
  82: pizzaBuilder.buildSauce();
  83: pizzaBuilder.buildTopping();
  84: }
  85: }
  86:  
  87: /** A given type of pizza being constructed. */
  88: public class BuilderExample {
  89: public static void main(String[] args) {
  90: Cook cook = new Cook();
  91: PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
  92: PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
  93:  
  94: cook.setPizzaBuilder(hawaiianPizzaBuilder);
  95: cook.constructPizza();
  96:  
  97: Pizza pizza = cook.getPizza();
  98: }
  99: }
posted on 2011-02-09 22:45 人在江湖 阅读(780) 评论(0)  编辑  收藏 所属分类: design pattern

只有注册用户登录后才能发表评论。


网站导航: