blob: 5aa70cdffb34cc51369240ba8f06edaeb7da1102 (
plain)
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
|
#include <string>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class Node { //abstract base class
public:
virtual string get_name() = 0; //signal output
};
class A_node: public Node {
public:
string get_name(){ return "A"; };
string get_occupation(){ return "an A"; };
};
class B_node: public Node {
public:
string get_name(){ return "B"; };
string get_occupation(){ return "a B"; };
};
class C_node: public Node {
public:
string get_name(){ return "C"; };
string get_occupation(){ return "a C"; };
};
class Node_factory {
public:
template <typename T>
T* clone(T* proto) {
return new T();
};
Node_factory() {
add_type("A",new A_node());
add_type("B",new B_node());
add_type("C",new C_node());
}
Node *get_node(string type) {
if (type=="A") return new A_node();
if (type=="B") return new B_node();
if (type=="C") return new C_node();
};
map<string,Node*> type_map;
void add_type(string type,Node *proto){
type_map[type]=proto;
};
Node *create(string type) {
if (type_map.find(type)!=type_map.end()) {
return (Node*)clone(type_map[type]);
}
}
};
int main() {
Node_factory f=Node_factory();
vector<Node*> nodes;
nodes.push_back(f.get_node("A"));
nodes.push_back(f.get_node("B"));
nodes.push_back(f.get_node("C"));
for (auto &i: nodes) {
cout << i->get_name() << endl;
}
}
|