• <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
            久久久这里只有精品加勒比| 久久国产精品久久| 四虎国产精品成人免费久久| 欧美日韩精品久久久久| 成人免费网站久久久| 久久久这里有精品中文字幕| 亚洲国产精品一区二区久久hs| 久久香蕉国产线看观看精品yw| 国产精品成人99久久久久| 狠狠色丁香久久婷婷综合| 亚洲伊人久久大香线蕉苏妲己| 欧美亚洲国产精品久久| 曰曰摸天天摸人人看久久久| 久久免费视频1| 久久久久久一区国产精品| 久久99国产综合精品| 一级女性全黄久久生活片免费 | 日日狠狠久久偷偷色综合0| 激情伊人五月天久久综合| 一本综合久久国产二区| 久久成人精品| 青青热久久综合网伊人| 亚洲精品无码久久千人斩| 性欧美大战久久久久久久| 26uuu久久五月天| 久久免费高清视频| 999久久久免费精品国产| 青青草原精品99久久精品66| 国产精品成人久久久| 色综合久久夜色精品国产| 免费一级做a爰片久久毛片潮| 国内精品久久久久久中文字幕| 久久综合久久综合九色| 成人国内精品久久久久影院| 国产韩国精品一区二区三区久久| 无码伊人66久久大杳蕉网站谷歌| 偷窥少妇久久久久久久久| 一本色道久久88—综合亚洲精品 | 热RE99久久精品国产66热| 久久久久国产一区二区三区| 久久97久久97精品免视看 |