設計模式(三)建造者模式(轉)

原文地址:http://www.jellythink.com/archives/98ios

C++設計模式——建造者模式

建造者模式

在GOF的《設計模式 可複用面向對象軟件的基礎》中是這樣說的:將一個複雜對象的構建與它的表示分離,使得一樣的構建過程能夠建立不一樣的表示。編程

這句話,似懂非懂的。一個複雜對象的建立,其一般是由不少的子對象構成;若是一個對象可以直接就建立好了,那麼也不會稱之爲複雜對象。因爲項目中需求的變化,這個複雜對象的各個部分常常會發生劇烈的變化,可是,無論怎麼變化,將它們組合在一塊兒,組成一個複雜的對象的事實是不會變的。建造者模式就提供了一種「封裝機制」來將各個對象的變化隔離開,最終,組合成複雜對象的過程是不會變的。設計模式

在《大話設計模式》一書中,例舉了一個很好的例子————建造小人。建造一個小人,要分爲六步:頭部、身體、左手、右手、左腳和右腳。與抽象工廠模式不一樣的是,建造者模式是在Director的控制下一步一步的構造出來的,在建造的過程當中,建造者模式能夠進行更精細的控制。無論人的頭部、身體、左手、右手、左腳或者右腳如何變化,可是最終仍是由這幾部分組合在一塊兒造成一我的,雖然是同一個建造過程,可是這我的就會有不一樣的表示,好比,胖子,瘦子,個高的,個低的等等。ide

UML圖

類圖以下:
果凍想 | 一個原創文章分享網站學習

時序圖以下:
果凍想 | 一個原創文章分享網站網站

代碼實現

  1 /*
  2 ** FileName     : BuilderPattern
  3 ** Author       : Jelly Young
  4 ** Date         : 2013/11/22
  5 ** Description  : More information, please go to http://www.jellythink.com
  6 */
  7 
  8 #include <iostream>
  9 using namespace std;
 10 
 11 typedef enum MANTYPETag
 12 {
 13     kFatMan,
 14     kThinMan,
 15     kNormal
 16 }MANTYPE;
 17 
 18 class Man
 19 {
 20 public:
 21     void SetHead(MANTYPE type){ m_Type = type; }
 22     void SetBody(MANTYPE type){ m_Type = type; }
 23     void SetLeftHand(MANTYPE type){ m_Type = type; }
 24     void SetRightHand(MANTYPE type){ m_Type = type; }
 25     void SetLeftFoot(MANTYPE type){ m_Type = type; }
 26     void SetRightFoot(MANTYPE type){ m_Type = type; }
 27     void ShowMan()
 28     {
 29         switch (m_Type)
 30         {
 31         case kFatMan:
 32             cout<<"I'm a fat man"<<endl;
 33             return;
 34 
 35         case kThinMan:
 36             cout<<"I'm a thin man"<<endl;
 37             return;
 38 
 39         default:
 40             cout<<"I'm a normal man"<<endl;
 41             return;
 42         }
 43     }
 44 
 45 private:
 46     MANTYPE m_Type;
 47 };
 48 
 49 // Builder
 50 class Builder
 51 {
 52 public:
 53     virtual void BuildHead(){}
 54     virtual void BuildBody(){}
 55     virtual void BuildLeftHand(){}
 56     virtual void BuildRightHand(){}
 57     virtual void BuildLeftFoot(){}
 58     virtual void BuildRightFoot(){}
 59     virtual Man *GetMan(){ return NULL; }
 60 };
 61 
 62 // FatManBuilder
 63 class FatManBuilder : public Builder
 64 {
 65 public:
 66     FatManBuilder(){ m_FatMan = new Man(); }
 67     void BuildHead(){ m_FatMan->SetHead(kFatMan); }
 68     void BuildBody(){ m_FatMan->SetBody(kFatMan); }
 69     void BuildLeftHand(){ m_FatMan->SetLeftHand(kFatMan); }
 70     void BuildRightHand(){ m_FatMan->SetRightHand(kFatMan); }
 71     void BuildLeftFoot(){ m_FatMan->SetLeftFoot(kFatMan); }
 72     void BuildRightFoot(){ m_FatMan->SetRightFoot(kFatMan); }
 73     Man *GetMan(){ return m_FatMan; }
 74 
 75 private:
 76     Man *m_FatMan;
 77 };
 78 
 79 // ThisManBuilder
 80 class ThinManBuilder : public Builder
 81 {
 82 public:
 83     ThinManBuilder(){ m_ThinMan = new Man(); }
 84     void BuildHead(){ m_ThinMan->SetHead(kThinMan); }
 85     void BuildBody(){ m_ThinMan->SetBody(kThinMan); }
 86     void BuildLeftHand(){ m_ThinMan->SetLeftHand(kThinMan); }
 87     void BuildRightHand(){ m_ThinMan->SetRightHand(kThinMan); }
 88     void BuildLeftFoot(){ m_ThinMan->SetLeftFoot(kThinMan); }
 89     void BuildRightFoot(){ m_ThinMan->SetRightFoot(kThinMan); }
 90     Man *GetMan(){ return m_ThinMan; }
 91 
 92 private:
 93     Man *m_ThinMan;
 94 };
 95 
 96 // Director
 97 class Director
 98 {
 99 public:
100     Director(Builder *builder) { m_Builder = builder; }
101     void CreateMan();
102 
103 private:
104     Builder *m_Builder;
105 };
106 
107 void Director::CreateMan()
108 {
109     m_Builder->BuildHead();
110     m_Builder->BuildBody();
111     m_Builder->BuildLeftHand();
112     m_Builder->BuildRightHand();
113     m_Builder->BuildLeftHand();
114     m_Builder->BuildRightHand();
115 }
116 
117 int main(int argc, char *argv[])
118 {
119     Builder *builderObj = new FatManBuilder();
120     Director directorObj(builderObj);
121     directorObj.CreateMan();
122     Man *manObj = builderObj->GetMan();
123     if (manObj == NULL)
124         return 0;
125 
126     manObj->ShowMan();
127 
128     delete manObj; // 感謝張小張同窗的review
129     manObj = NULL;
130 
131     delete builderObj;
132     builderObj = NULL;
133 
134     return 0;
135 };
View Code

上面這個例子比較雜,可是也是建造者模式的應用。下面這個例子是建造者最通常,最簡單的實現方法:ui

 1 /*
 2 ** FileName     : BuilderPattern
 3 ** Author       : Jelly Young
 4 ** Date         : 2013/11/23
 5 ** Description  : More information, please go to http://www.jellythink.com
 6 */
 7 
 8 #include <iostream>
 9 #include <vector>
10 using namespace std;
11 
12 class Builder;
13 
14 // Product
15 class Product
16 {
17 public:
18     void AddPart(const char *info) { m_PartInfoVec.push_back(info); }
19     void ShowProduct()
20     {
21         for (std::vector<const char *>::iterator item = m_PartInfoVec.begin(); 
22             item != m_PartInfoVec.end(); ++item)
23         {
24             cout<<*item<<endl;
25         }
26     }
27 
28 private:
29     std::vector<const char *> m_PartInfoVec;
30 };
31 
32 // Builder
33 class Builder
34 {
35 public:
36     virtual void BuildPartA() {}
37     virtual void BuildPartB() {}
38     virtual Product *GetProduct() { return NULL; }
39 };
40 
41 // ConcreteBuilder
42 class ConcreteBuilder : public Builder
43 {
44 public:
45     ConcreteBuilder() { m_Product = new Product(); }
46     void BuildPartA()
47     {
48         m_Product->AddPart("PartA completed");
49     }
50 
51     void BuildPartB()
52     {
53         m_Product->AddPart("PartB completed");
54     }
55 
56     Product *GetProduct() { return m_Product; }
57 
58 private:
59     Product *m_Product;
60 };
61 
62 // Director
63 class Director
64 {
65 public:
66     Director(Builder *builder) { m_Builder = builder; }
67     void CreateProduct()
68     {
69         m_Builder->BuildPartA();
70         m_Builder->BuildPartB();
71     }
72 
73 private:
74     Builder *m_Builder;
75 };
76 
77 // main
78 int main()
79 {
80     Builder *builderObj = new ConcreteBuilder();
81     Director directorObj(builderObj);
82     directorObj.CreateProduct();
83     Product *productObj = builderObj->GetProduct();
84     if (productObj == NULL)
85     {
86         return 0;
87     }
88     productObj->ShowProduct();
89 
90         delete productObj;
91         productObj = NULL; // 謝謝賓零同窗的review
92     delete builderObj;
93     builderObj = NULL;
94 }
View Code

經過比較上面的兩個例子,能夠很容易的把建造者模式的骨架抽象出來。spa

使用要點

  1. 建造者模式生成的對象有複雜的內部結構,將分步驟的去構建一個複雜的對象,分多少步是肯定的,而每一步的實現是不一樣的,可能常常發生變化;
  2. 在上面的例子中,咱們都看到了最終生成的Man和Product都沒有抽象類,這又導出建造者適用的一種狀況,當須要建立複雜對象的過程當中,複雜對象沒有多少共同的特色,很難抽象出來時,而複雜對象的組裝又有必定的類似點時,建造者模式就能夠發揮出做用。簡單的說,可能使用了建造者模式,最終建造的對象可能沒有多大的關係,關於這一點,閱讀《設計模式 可複用面向對象軟件的基礎》中的建造者模式時是最有體會的。

總結

一個複雜對象是由多個部件組成的,建造者模式是把複雜對象的建立和部件的建立分別開來,分別用Builder類和Director類來表示。用Director構建最後的複雜對象,而在上面Builder接口中封裝的是如何建立一個個部件(複雜對象是由這些部件組成的),也就是說,Director負責如何將部件最後組裝成產品。這樣建造者模式就讓設計和實現解耦了。設計

剛開始接觸建造者模式的時候,最容易把建造者和抽象工廠模式混淆了。因爲而這都屬於建立型的設計模式,因此兩者之間是有公共點的,可是建造者模式注重於對象組合,即不一樣的小對象組成一個總體的複雜大對象,而抽象工廠模式針對於接口編程,只是對外提供建立對象的工廠接口,不負責對象以後的處理。code

建造者模式,是一個比較複雜,不容易權衡的設計模式。你們應該更多的閱讀開源代碼,理解他人是如何使用該模式的。從實際的應用中學習設計模式。

相關文章
相關標籤/搜索