-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype.cpp
More file actions
110 lines (87 loc) · 2.24 KB
/
prototype.cpp
File metadata and controls
110 lines (87 loc) · 2.24 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Prototype design pattern support to clone objects (including very big and complicated objects).
And with this pattern, it will not depend on the class of those objects.
*/
#include <iostream>
#include <map>
using namespace std;
enum type
{
PROTOTYPE_1,
PROTOTYPE_2
};
// prototype class interface that support clone method
class protoType
{
public:
protoType() {}
protoType(string name) : _name(name) {}
virtual ~protoType() {}
virtual protoType *clone() const = 0;
virtual void method (int id)
{
_idx = id;
cout << "Call method from prototype: " << _name << ", index: " << _idx << endl;
}
protected:
string _name;
int _idx;
};
class concreteProtoType1 : public protoType
{
public:
// initialize the data members of both base class and derived class
concreteProtoType1(string name, int id) : protoType(name), _idx1(id) {}
protoType *clone() const override { return new concreteProtoType1(*this); }
private:
int _idx1;
};
class concreteProtoType2 : public protoType
{
public:
// initialize the data members of both base class and derived class
concreteProtoType2(string name, int id) : protoType(name), _idx2(id) {}
protoType *clone() const override { return new concreteProtoType2(*this); }
private:
int _idx2;
};
class prototypeFactory
{
public:
prototypeFactory()
{
_protoMap[type::PROTOTYPE_1] = new concreteProtoType1("PROTOTYPE_1", 50);
_protoMap[type::PROTOTYPE_2] = new concreteProtoType2("PROTOTYPE_2", 60);
}
~prototypeFactory()
{
delete _protoMap[type::PROTOTYPE_1];
delete _protoMap[type::PROTOTYPE_2];
}
protoType *createProtoType(type typ)
{
return _protoMap[typ]->clone();
}
private:
map<type, protoType *> _protoMap;
};
void demo(prototypeFactory *prototypeFact)
{
cout << "Let's create prototype 1: " << endl;
protoType * typ = prototypeFact->createProtoType(type::PROTOTYPE_1);
typ->method(90);
delete typ;
cout << "Let's create prototype 2: " << endl;
typ = prototypeFact->createProtoType(type::PROTOTYPE_2);
typ->method(10);
delete typ;
return;
};
int main()
{
prototypeFactory *fact = new prototypeFactory();
demo(fact);
delete fact;
fact = nullptr;
return 0;
}