9 建造者模式

好菜没回味道不同——建造者模式

建造者模式(Builder),又声称其模式,讲一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

 

建造者模式的代码
1 #include <iostream>
2
3  using std::cout;
4  using std::endl;
5
6  //抽象的建造人的类
7  class PersonBuilder
8 {
9  public:
10 virtual void BuildHead() = 0;
11 virtual void BuildBody() = 0;
12 virtual void BuildArmLeft() = 0;
13 virtual void BuildArmRight() = 0;
14 virtual void BuildLegLeft() = 0;
15 virtual void BuildLegRight() = 0;
16
17 };
18
19  //具体建造者类,建造瘦人
20 //因为抽象建造类中的建造方法都是纯虚函数
21 //这就要求具体建造类中必须一个不漏的全部重写
22 //否则生成具体对象时会编译出错
23 class PersonThinBuilder : public PersonBuilder
24 {
25 public:
26 void BuildHead()
27 {
28 cout<<"绘制瘦人的Head"<<endl;
29 }
30 void BuildBody()
31 {
32 cout<<"绘制瘦人的Body"<<endl;
33 }
34 void BuildArmLeft()
35 {
36 cout<<"绘制瘦人的ArmLeft"<<endl;
37
38 }
39 void BuildArmRight()
40 {
41 cout<<"绘制瘦人的ArmRight"<<endl;
42 }
43 void BuildLegLeft()
44 {
45 cout<<"绘制瘦人的LegLeft"<<endl;
46 }
47 void BuildLegRight()
48 {
49 cout<<"绘制瘦人的LegRight"<<endl;
50 }
51 };
52
53 class PersonFatBuilder : public PersonBuilder
54 {
55 public:
56 void BuildHead()
57 {
58 cout<<"绘制胖人的Head"<<endl;
59 }
60 void BuildBody()
61 {
62 cout<<"绘制胖人的Body"<<endl;
63 }
64 void BuildArmLeft()
65 {
66 cout<<"绘制胖人的ArmLeft"<<endl;
67
68 }
69 void BuildArmRight()
70 {
71 cout<<"绘制胖人的ArmRight"<<endl;
72 }
73 void BuildLegLeft()
74 {
75 cout<<"绘制胖人的LegLeft"<<endl;
76 }
77 void BuildLegRight()
78 {
79 cout<<"绘制胖人的LegRight"<<endl;
80 }
81 };
82
83 //指挥者类,用来控制建造过程,也用它来隔离用户与建造过程的关联
84 //用户不需要意义调用Build*函数,而只需要调用一个Create接口
85 //避免了用户可能遗漏某些部分函数调用的情况
86 class PersonDirector
87 {
88 public:
89 PersonDirector(PersonBuilder* pb)
90 {
91 this->pb = pb;
92 }
93 void CreatePerson()
94 {
95 pb->BuildHead();
96 pb->BuildBody();
97 pb->BuildArmLeft();
98 pb->BuildArmRight();
99 pb->BuildLegLeft();
100 pb->BuildLegRight();
101 }
102 private:
103 PersonBuilder *pb;
104
105 };
106
107 int main()
108 {
109 PersonThinBuilder *ptb = new PersonThinBuilder();
110 PersonDirector *pdThin = new PersonDirector(ptb);
111 pdThin->CreatePerson();
112
113 cout<<endl;
114
115 PersonFatBuilder *pfb = new PersonFatBuilder();
116 PersonDirector *pdFat = new PersonDirector(pfb);
117 pdFat->CreatePerson();
118 return 0;
119 }

 

建造者模式的基本代码
1 #include <iostream>
2 #include <string>
3 #include <list>
4
5 using std::cout;
6 using std::endl;
7 using std::string;
8 using std::list;
9 using std::iterator;
10
11 //产品类
12 class Product
13 {
14 public:
15 //添加产品部件
16 void Add(string part)
17 {
18 parts.push_back(part);
19 }
20 void Show()
21 {
22 cout<<"产品 创建——"<<endl;
23 for(list<string>::iterator iter = parts.begin(); iter != parts.end(); ++iter)
24 {
25 cout<<*iter<<endl;
26 }
27 }
28 private:
29 list<string> parts;
30 };
31
32 //Builder类——抽象建造者类
33 class Builder
34 {
35 public:
36 virtual void BuilderPartA() = 0;
37 virtual void BuilderPartB() = 0;
38 virtual Product* GetResult() = 0;
39 };
40
41 //ConcreteBuilder1——具体建造者类
42 class ConcreteBuilder1 : public Builder
43 {
44 public:
45 ConcreteBuilder1()
46 {
47 product = new Product();
48 }
49 void BuilderPartA()
50 {
51 product->Add("部件A");
52 }
53 void BuilderPartB()
54 {
55 product->Add("部件B");
56 }
57 Product* GetResult()
58 {
59 return product;
60 }
61 private:
62 Product *product;
63 };
64
65 //ConcreteBuilder2——具体建造者类
66 class ConcreteBuilder2 : public Builder
67 {
68 public:
69 ConcreteBuilder2()
70 {
71 product = new Product();
72 }
73 void BuilderPartA()
74 {
75 product->Add("部件X");
76 }
77 void BuilderPartB()
78 {
79 product->Add("部件Y");
80 }
81 Product* GetResult()
82 {
83 return product;
84 }
85 private:
86 Product *product;
87 };
88
89 //Director类——指挥者类
90 class Director
91 {
92 public:
93 void Construct(Builder *builder)
94 {
95 builder->BuilderPartA();
96 builder->BuilderPartB();
97 }
98 };
99
100 int main()
101 {
102 Director *director = new Director();
103
104 Builder *b1 = new ConcreteBuilder1();
105 director->Construct(b1);//实际的建造过程
106 Product *p1 = b1->GetResult();
107 p1->Show ();
108
109 cout<<endl;
110
111 Builder *b2 = new ConcreteBuilder2();
112 director->Construct(b2);//实际的建造过程
113 Product *p2 = b2->GetResult();
114 p2->Show ();
115
116 return 0;
117 }

 

 

 

 

posted @ 2010-05-16 16:24  斯芬克斯  阅读(271)  评论(0编辑  收藏  举报