• <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 老馬驛站 閱讀(358) 評論(0)  編輯 收藏 引用 所屬分類: Design pattern
            久久久久久综合网天天| 97精品伊人久久大香线蕉| 日产精品久久久一区二区| 日韩精品久久无码人妻中文字幕 | 亚洲精品成人久久久| 99精品久久久久久久婷婷| 国产亚洲精午夜久久久久久| 中文字幕无码免费久久| 91精品国产91久久久久福利| 久久精品一区二区三区AV| 亚洲国产精品无码成人片久久| 伊人 久久 精品| 99久久精品午夜一区二区| 久久久久久久女国产乱让韩| 国产亚洲精品美女久久久| 日韩电影久久久被窝网| 国产成人精品久久综合 | 99久久精品日本一区二区免费| 蜜桃麻豆www久久| 国产精品久久精品| 久久人人爽人爽人人爽av | 久久婷婷五月综合97色直播| 国产精品久久自在自线观看| 亚洲&#228;v永久无码精品天堂久久 | 久久午夜无码鲁丝片午夜精品| 色婷婷综合久久久久中文| 亚洲国产婷婷香蕉久久久久久| 精品人妻久久久久久888| 久久婷婷五月综合国产尤物app| 亚洲精品视频久久久| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 996久久国产精品线观看| 国产69精品久久久久APP下载| 久久天天躁狠狠躁夜夜不卡 | 亚洲综合精品香蕉久久网97| 国内精品久久久久影院一蜜桃| 亚洲欧美一区二区三区久久| 久久久精品视频免费观看| 亚洲国产成人精品91久久久 | 99精品国产免费久久久久久下载| 久久www免费人成精品香蕉|