forked from FengJungle/DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrototypePattern.h
More file actions
75 lines (66 loc) · 1.38 KB
/
PrototypePattern.h
File metadata and controls
75 lines (66 loc) · 1.38 KB
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
66
67
68
69
70
71
72
73
74
75
#ifndef __PROTOTYPE_PATTERN__
#define __PROTOTYPE_PATTERN__
#include <iostream>
#include <string.h>
using namespace std;
// work model类
class WorkModel
{
public:
char *modelName;
void setWorkModelName(char *iName){
this->modelName = iName;
}
};
// 抽象原型类PrototypeWork
class PrototypeWork
{
public:
PrototypeWork(){}
virtual ~PrototypeWork(){}
virtual PrototypeWork *clone() = 0;
private:
};
// 具体原型类PrototypeWork
class ConcreteWork :public PrototypeWork
{
public:
ConcreteWork(){}
ConcreteWork(char* iName, int iIdNum, char* modelName){
this->name = iName;
this->idNum = iIdNum;
this->workModel = new WorkModel();
this->workModel->setWorkModelName(modelName);
}
ConcreteWork *clone(){
ConcreteWork *work = new ConcreteWork();
work->setName(this->name);
work->setIdNum(this->idNum);
work->workModel = this->workModel;
return work;
}
~ConcreteWork(){
delete workModel;
workModel = nullptr;
}
void setName(char* iName){
this->name = iName;
}
void setIdNum(int iIdNum){
this->idNum = iIdNum;
}
void setModel(WorkModel *iWorkModel){
this->workModel = iWorkModel;
}
// 打印work信息
void printWorkInfo(){
printf("name:%s\t\n", this->name);
printf("idNum:%d\t\n", this->idNum);
printf("modelName:%s\t\n", this->workModel->modelName);
}
private:
char* name;
int idNum;
WorkModel *workModel;
};
#endif //__PROTOTYPE_PATTERN__