首页 > 代码库 > 建造者模式

建造者模式

【1】什么是建造者模式?

将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

【2】建造者模式代码示例:

代码示例1:

 1 #include <string> 2 #include <iostream> 3 #include <vector> 4 using namespace std; 5  6 class Person 7 { 8 public: 9     virtual void createHead() = 0;10     virtual void createHand() = 0;11     virtual void createBody() = 0;12     virtual void createFoot() = 0;13 };14 15 class ThinPerson : public Person16 {17     void createHead()18     {19         cout << "thin head" << endl;20     }21     void createHand()22     {23         cout << "thin hand" << endl;24     }25     void createBody()26     {27         cout << "thin body" << endl;28     }29     void createFoot()30     {31         cout << "thin foot" << endl;32     }33 };34 35 class FatPerson : public Person36 {37     void createHead()38     {39         cout << "fat head" << endl;40     }41     void createHand()42     {43         cout << "fat hand" << endl;44     }45     void createBody()46     {47         cout << "fat body" << endl;48     }49     void createFoot()50     {51         cout << "fat foot" << endl;52     }53 };54 55 56 class Director57 {58 private:59     Person *p;60 public:61     Director(Person *temp)62     {63         p = temp;64     }65     void create()66     {67         p->createHead();68         p->createHand();69         p->createBody();70         p->createFoot();71     }72 };73 74 //客户端代码:75 int main()76 {77     Person *p = new FatPerson();78     Person *b = new ThinPerson();79     Director *d = new Director(p);80     Director *s = new Director(b);81     d->create();82     s->create();83     delete d;84     delete p;85     delete s;86     delete b;87 88     return 0;89 }
View Code

代码示例2:

  1 #include <string>  2 #include <iostream>  3 #include <vector>  4 using namespace std;  5   6 class Product  7 {  8 private:  9     vector<string> product; 10 public: 11     void add(string str) 12     { 13         product.push_back(str); 14     } 15     void show() 16     { 17         vector<string>::iterator iter = product.begin(); 18         while (iter != product.end()) 19         { 20             cout << *iter << "  "; 21             ++iter; 22         } 23         cout << endl; 24     } 25 }; 26  27 class Builder 28 { 29 public: 30     virtual void builderA() = 0; 31     virtual void builderB() = 0; 32     virtual Product *getResult() = 0; 33 }; 34  35 class ConcreteBuilder1 : public Builder 36 { 37 private: 38     Product *product; 39 public: 40     ConcreteBuilder1() 41     { 42         product = new Product(); 43     } 44     virtual void builderA() 45     { 46         product->add("one"); 47     } 48     virtual void builderB() 49     { 50         product->add("two"); 51     } 52     virtual Product *getResult() 53     { 54         return product; 55     } 56 }; 57  58  59 class ConcreteBuilder2 : public Builder 60 { 61 private: 62     Product *product; 63 public: 64     ConcreteBuilder2() 65     { 66         product = new Product(); 67     } 68     virtual void builderA() 69     { 70         product->add("A"); 71     } 72     virtual void builderB() 73     { 74         product->add("B"); 75     } 76     virtual Product *getResult() 77     { 78         return product; 79     } 80 }; 81  82 class Director 83 { 84 private: 85     Product *p; 86 public: 87     void construct(Builder *bd) 88     { 89         bd->builderA(); 90         bd->builderB(); 91         p = bd->getResult(); 92     } 93     Product *getResult() 94     { 95         return p; 96     } 97 }; 98  99 int main()100 {101     Director *director = new Director();102 103     Builder *bd1 = new ConcreteBuilder1();104     director->construct(bd1);105     Product *pbd1 = director->getResult();106 107     pbd1->show();108 109     return 0;110 }
View Code

 

Good  Good  Study,  Day  Day  Up.

顺序   选择   循环    总结

建造者模式