-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFactory.cpp
79 lines (66 loc) · 1.58 KB
/
Factory.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <map>
#include <utility>
class Worker {
};
class Creator {
public:
virtual Worker *create() {};
};
class Student : public Worker {
};
class StudentCreator : public Creator {
public:
Worker *create() override {
return new Student;
}
};
class Teacher : public Worker {
};
class TeacherCreator : public Creator {
public:
Worker *create() override {
return new Teacher;
}
};
class Admin : public Worker {
};
class AdminCreator : public Creator {
public:
Worker *create() override {
return new Admin;
}
};
class WorkersFactory {
public:
std::map<std::string, Creator *> FactoryMap;
WorkersFactory() {
std::pair<std::string, Creator *> p;
p.first = "Student";
p.second = new StudentCreator;
FactoryMap.insert(p);
p.first = "Teacher";
p.second = new TeacherCreator;
FactoryMap.insert(p);
p.first = "Admin";
p.second = new AdminCreator;
FactoryMap.insert(p);
}
Worker *createProduct(std::string type) {
auto c = FactoryMap.find(type);
if (c != FactoryMap.end()) {
c->second->create();
std::cout << type << " was created" << std::endl;
} else {
std::cout << "wrong Type" << std::endl;
}
}
};
//int main() {
// WorkersFactory *f = new WorkersFactory();
// Worker *s = f->createProduct("Student");
// Worker *t = f->createProduct("Teacher");
// Worker *a = f->createProduct("Admin");
// Worker *e = f->createProduct("Lecture");
// return 0;
//}