建造者模式

【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 Person
 16 {
 17     void createHead();
 18     void createHand();
 19     void createBody();
 20     void createFoot();
 21 };
 22 
 23 void ThinPerson::createHead()
 24 {
 25     cout << "thin head" << endl;
 26 }
 27 void ThinPerson::createHand()
 28 {
 29     cout << "thin hand" << endl;
 30 }
 31 void ThinPerson::createBody()
 32 {
 33     cout << "thin body" << endl;
 34 }
 35 void ThinPerson::createFoot()
 36 {
 37     cout << "thin foot" << endl;
 38 }
 39 
 40 class FatPerson : public Person
 41 {
 42     void createHead();
 43     void createHand();
 44     void createBody();
 45     void createFoot();
 46 };
 47 
 48 void FatPerson::createHead()
 49 {
 50     cout << "fat head" << endl;
 51 }
 52 void FatPerson::createHand()
 53 {
 54     cout << "fat hand" << endl;
 55 }
 56 void FatPerson::createBody()
 57 {
 58     cout << "fat body" << endl;
 59 }
 60 void FatPerson::createFoot()
 61 {
 62     cout << "fat foot" << endl;
 63 }
 64 
 65 class Director
 66 {
 67 private:
 68     Person *m_pObj;
 69 
 70 public:
 71     Director(Person *pTemp) : m_pObj(pTemp)
 72     {}
 73 
 74     void construct()
 75     {
 76         m_pObj->createHead();
 77         m_pObj->createHand();
 78         m_pObj->createBody();
 79         m_pObj->createFoot();
 80     }
 81 };
 82 
 83 // 客户端代码:
 84 void main()
 85 {
 86     Person *pFat = new FatPerson();
 87     Person *pThin = new ThinPerson();
 88 
 89     Director dctorFat(pFat);
 90     dctorFat.construct();
 91 
 92     Director dctorThin(pThin);
 93     dctorThin.construct();
 94 
 95     delete pFat;
 96     delete pThin;
 97 
 98     system("pause");
 99 }
100 // run out
101 /*
102 fat head
103 fat hand
104 fat body
105 fat foot
106 thin head
107 thin hand
108 thin body
109 thin foot
110 请按任意键继续. . .
111 */

代码示例2:

  1 #include <string>
  2 #include <vector>
  3 #include <iostream>
  4 using namespace std;
  5 
  6 class Product
  7 {
  8 private:
  9     vector<string> m_vecProduct;
 10 
 11 public:
 12     void add(string str)
 13     {
 14         m_vecProduct.push_back(str);
 15     }
 16 
 17     void show()
 18     {
 19         vector<string>::iterator iter = m_vecProduct.begin();
 20         while (iter != m_vecProduct.end())
 21         {
 22             cout << *iter << "  ";
 23             ++iter;
 24         }
 25         cout << endl;
 26     }
 27 };
 28 
 29 class Builder
 30 {
 31 public:
 32     virtual void builderA() = 0;
 33     virtual void builderB() = 0;
 34     virtual Product *getResult() = 0;
 35 };
 36 
 37 class ConcreteBuilder1 : public Builder
 38 {
 39 private:
 40     Product *m_pProduct;
 41 
 42 public:
 43     ConcreteBuilder1()
 44     {
 45         m_pProduct = new Product();
 46     }
 47     virtual void builderA()
 48     {
 49         m_pProduct->add("one");
 50     }
 51     virtual void builderB()
 52     {
 53         m_pProduct->add("two");
 54     }
 55     virtual Product *getResult()
 56     {
 57         return m_pProduct;
 58     }
 59 };
 60 
 61 
 62 class ConcreteBuilder2 : public Builder
 63 {
 64 private:
 65     Product *m_pProduct;
 66 
 67 public:
 68     ConcreteBuilder2()
 69     {
 70         m_pProduct = new Product();
 71     }
 72     virtual void builderA()
 73     {
 74         m_pProduct->add("AA");
 75     }
 76     virtual void builderB()
 77     {
 78         m_pProduct->add("BB");
 79     }
 80     virtual Product *getResult()
 81     {
 82         return m_pProduct;
 83     }
 84 };
 85 
 86 class Director
 87 {
 88 private:
 89     Product *m_pProduct;
 90 
 91 public:
 92     void construct(Builder *pBd)
 93     {
 94         pBd->builderA();
 95         pBd->builderB();
 96         m_pProduct = pBd->getResult();
 97     }
 98     Product *getResult()
 99     {
100         return m_pProduct;
101     }
102 };
103 
104 void main()
105 {
106     Director *pDirector = new Director();
107 
108     Builder *pBd1 = new ConcreteBuilder1();
109     pDirector->construct(pBd1);
110     Product *pbd1 = pDirector->getResult();
111     pbd1->show();
112 
113     Builder *pBd2 = new ConcreteBuilder2();
114     pDirector->construct(pBd2);
115     Product *pbd2 = pDirector->getResult();
116     pbd2->show();
117 
118     delete pBd1;
119     delete pBd2;
120     delete pDirector;
121 
122     system("pause");
123 }
124 // run out
125 /*
126 one  two
127 AA  BB
128 请按任意键继续. . .
129 */

【3】建造者模式的优缺点

(1)优点

1、将复杂产品的创建步骤分解在不同的方法中,使得创建过程更加清晰,使得我们能够更加精确的控制复杂对象的产生过程。

2、将产品的创建过程与产品本身分离开来,可以使用相同的创建过程来得到不同的产品。也就说细节依赖抽象。

3、每一个具体建造者都相对独立,而与其他的具体建造者无关,因此可以很方便地替换具体建造者或增加新的具体建造者,用户使用不同的具体建造者即可得到不同的产品对象。

(2)缺点

1、建造者模式所创建的产品一般具有较多的共同点,其组成部分相似,如果产品之间的差异性很大,则不适合使用建造者模式,因此其使用范围受到一定的限制。

2、如果产品的内部变化复杂,可能会导致需要定义很多具体建造者类来实现这种变化,导致系统变得很庞大。

 

Good Good Study, Day Day Up.

顺序 选择 循环 总结

posted @ 2014-08-28 21:59  kaizenly  阅读(312)  评论(0编辑  收藏  举报
打赏