-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder mode.cpp
65 lines (60 loc) · 1.6 KB
/
builder mode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include <vector>
#include <string>
using namespace std;
//创建者模式
//细节在builde或其子类中实现,而完成整个工程由director指近builder的主法来完成。
class Builder //end of class need ; always,to seperate class and fuction
{
public://all abstrcat mehtod need to be instantiation in all the subclass
virtual void BuildHead() = 0;
virtual void BuildArm() = 0;
};
class FatBuilder: public Builder
{
private:
string m_fatname;
public:
FatBuilder(string name)
{
m_fatname = name;
}
FatBuilder()
{
m_fatname = "无名" ;
}
virtual void BuildHead()
{
cout<<"build a fat man"<<m_fatname<<endl;
};
virtual void BuildArm()
{
cout << "build a big arm"<<endl;
}
};
class Director //not inherit from Builder but contain one Builder Object
{
private:
Builder *m_builder;
public:
Director(Builder *arg1)
{
m_builder = arg1;
}
//特别是当CreatMan需要复杂算法时,而这些算法由具体build组成时。
void CreatMan()//包含一个Bulder使用它的方法实现CreatMan
{
m_builder->BuildHead();
m_builder->BuildArm();
}
};
int main()
{
Builder *bp = new FatBuilder("小李");
Director *bd = new Director(bp);
bd->CreatMan();//发命令建造一个人。。
delete(bp);
delete(bd);
system("pause");
return 0;
}