-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype.h
More file actions
81 lines (70 loc) · 1.35 KB
/
prototype.h
File metadata and controls
81 lines (70 loc) · 1.35 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
#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
enum type
{
TYPEA = 0,
TYPEB
};
class prototype
{
protected:
string name;
int field;
public:
prototype(string _name) : name(_name)
{
}
virtual prototype* clone() const = 0;
virtual void method(int fileld)
{
this->field = fileld;
cout << "call method from " << name << " with field : " << fileld << endl;
}
};
class concrete_prototype1 : public prototype {
private:
int field1;
public:
concrete_prototype1(string _name, int _field) : prototype(_name), field1(_field)
{
}
prototype* clone() const override
{
return new concrete_prototype1(*this);
}
};
class concrete_prototype2 : public prototype {
private:
int field2;
public:
concrete_prototype2(string _name, int _field) : prototype(_name), field2(_field)
{
}
prototype* clone() const override
{
return new concrete_prototype2(*this);
}
};
class prototype_factory
{
private :
std::unordered_map<type, prototype*, std::hash<int>> prototypes;
public:
prototype_factory()
{
prototypes[type::TYPEA] = new concrete_prototype1("TYPEA", 50);
prototypes[type::TYPEB] = new concrete_prototype2("TYPEB", 30);
}
~prototype_factory()
{
delete prototypes[type::TYPEA];
delete prototypes[type::TYPEB];
}
prototype* create_prototype(type _type)
{
return prototypes[_type]->clone();
}
};