builder(转载)

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: }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值