• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-167  評論-8  文章-0  trackbacks-0
            The intent of the Builder design pattern is to separate the construction of a complex object from its representation. By doing so, the same construction process can create different representations.
            struct:

            Builder
            Abstract interface for creating objects (product).
            Concrete Builder
            Provides implementation for Builder. It is an object able to construct other objects. Constructs and assembles parts to build the objects.
              1 #include <string>
              2 #include <iostream>
              3 using namespace std;
              4  
              5 // "Product"
              6 class Pizza {
              7 public:
              8         void dough(const string& dough) {
              9                 dough_ = dough;
             10         }
             11  
             12         void sauce(const string& sauce) {
             13                 sauce_ = sauce;
             14         }
             15  
             16         void topping(const string& topping) {
             17                 topping_ = topping;
             18         }
             19  
             20         void open() const {
             21                 cout << "Pizza with " << dough_ << " dough, " << sauce_ << " sauce and "
             22                         << topping_ << " topping. Mmm." << endl;
             23         }
             24  
             25 private:
             26         string dough_;
             27         string sauce_;
             28         string topping_;
             29 };
             30  
             31 // "Abstract Builder"
             32 class PizzaBuilder {
             33 public:
             34         const Pizza& pizza() {
             35                 return pizza_;
             36         }
             37  
             38         virtual void buildDough() = 0;
             39         virtual void buildSauce() = 0;
             40         virtual void buildTopping() = 0;
             41  
             42 protected:
             43         Pizza pizza_;
             44 };
             45  
             46 //----------------------------------------------------------------
             47  
             48 class HawaiianPizzaBuilder : public PizzaBuilder {
             49 public:
             50         void buildDough() {
             51                 pizza_.dough("cross");
             52         }
             53  
             54         void buildSauce() {
             55                 pizza_.sauce("mild");
             56         }
             57  
             58         void buildTopping() {
             59                 pizza_.topping("ham+pineapple");
             60         }
             61 };
             62  
             63 class SpicyPizzaBuilder : public PizzaBuilder {
             64 public:
             65         void buildDough() {
             66                 pizza_.dough("pan baked");
             67         }
             68  
             69         void buildSauce() {
             70                 pizza_.sauce("hot");
             71         }
             72  
             73         void buildTopping() {
             74                 pizza_.topping("pepperoni+salami");
             75         }
             76 };
             77  
             78 //----------------------------------------------------------------
             79  
             80 class Cook {
             81 public:
             82         Cook()
             83                 : pizzaBuilder_(nullptr)
             84         {       }
             85  
             86         ~Cook() {
             87                 if (pizzaBuilder_)
             88                         delete pizzaBuilder_;
             89         }
             90  
             91         void pizzaBuilder(PizzaBuilder* pizzaBuilder) {
             92                 if (pizzaBuilder_)
             93                         delete pizzaBuilder_;
             94  
             95                 pizzaBuilder_ = pizzaBuilder;
             96         }
             97  
             98         const Pizza& getPizza() {
             99                 return pizzaBuilder_->pizza();
            100         }
            101  
            102         void constructPizza() {
            103                 pizzaBuilder_->buildDough();
            104                 pizzaBuilder_->buildSauce();
            105                 pizzaBuilder_->buildTopping();
            106         }
            107  
            108 private:
            109         PizzaBuilder* pizzaBuilder_;
            110 };
            111  
            112 int main() {
            113         Cook cook;
            114         cook.pizzaBuilder(new HawaiianPizzaBuilder);
            115         cook.constructPizza();
            116  
            117         Pizza hawaiian = cook.getPizza();
            118         hawaiian.open();
            119  
            120         cook.pizzaBuilder(new SpicyPizzaBuilder);
            121         cook.constructPizza();
            122  
            123         Pizza spicy = cook.getPizza();
            124         spicy.open();
            125 }
            posted on 2012-11-13 17:44 老馬驛站 閱讀(372) 評論(0)  編輯 收藏 引用 所屬分類: Design pattern
            国内精品伊人久久久久av一坑| 久久久久久久亚洲Av无码| 大香网伊人久久综合网2020| 亚洲国产精品一区二区久久| 一级做a爱片久久毛片| 久久久国产精品| 久久人爽人人爽人人片AV| 久久国产精品免费一区| 久久久这里只有精品加勒比| 久久不见久久见免费视频7| 成人亚洲欧美久久久久| 无码AV中文字幕久久专区| 99久久国产综合精品网成人影院| 老男人久久青草av高清| 国内精品久久久久国产盗摄| 久久国产精品无码一区二区三区| 久久无码精品一区二区三区| 99久久99久久精品免费看蜜桃| 久久久亚洲AV波多野结衣| 66精品综合久久久久久久| 久久夜色精品国产噜噜亚洲AV| 三级韩国一区久久二区综合| 青青热久久综合网伊人| 久久国产色AV免费观看| 久久久久青草线蕉综合超碰| 一本久久a久久精品综合夜夜| 亚洲va久久久噜噜噜久久狠狠| 久久人搡人人玩人妻精品首页| 国内精品欧美久久精品| 久久精品国产精品亚洲精品 | 久久精品国产99国产精偷| 77777亚洲午夜久久多喷| 亚洲欧洲久久av| 久久亚洲国产成人精品无码区| 久久综合狠狠综合久久激情 | 久久精品免费观看| 91久久精品无码一区二区毛片| 精品久久久久久久久久久久久久久| 99久久精品免费观看国产| 国产精品九九久久免费视频 | 色综合久久天天综合|