• <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 老馬驛站 閱讀(351) 評論(0)  編輯 收藏 引用 所屬分類: Design pattern
            国产午夜免费高清久久影院| 国产精品免费久久久久影院| 亚洲愉拍99热成人精品热久久| 伊人久久大香线蕉av不卡| 国产一级持黄大片99久久| 品成人欧美大片久久国产欧美| 少妇久久久久久被弄到高潮| 亚洲综合熟女久久久30p| 久久99国产精品一区二区| 日本精品久久久久久久久免费| 777午夜精品久久av蜜臀| 91精品久久久久久无码| 老男人久久青草av高清| 国产成人久久777777| 亚洲精品无码久久久久sm| 精品无码久久久久久国产| 久久精品国产第一区二区三区| 久久国内免费视频| 99久久国产综合精品成人影院| 亚洲伊人久久精品影院| 亚洲日韩欧美一区久久久久我 | 精品多毛少妇人妻AV免费久久| 蜜桃麻豆WWW久久囤产精品| 久久er国产精品免费观看8| 久久国产精品-久久精品| 国产成年无码久久久免费| 一级a性色生活片久久无| 国产免费久久精品丫丫| 久久久久久久尹人综合网亚洲| 亚洲日韩中文无码久久| 欧美黑人激情性久久| 久久午夜免费视频| 久久夜色精品国产| 久久久久久一区国产精品| 国产综合成人久久大片91| 国内精品久久国产大陆| 99久久人妻无码精品系列| AV狠狠色丁香婷婷综合久久| AAA级久久久精品无码片| 2021精品国产综合久久| 久久精品国产亚洲av麻豆小说|