-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory.cpp
More file actions
77 lines (67 loc) · 1.42 KB
/
Factory.cpp
File metadata and controls
77 lines (67 loc) · 1.42 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
//
// Created by osher on 13/02/2022.
//
#include <iostream>
#include <map>
#include <functional>
using namespace std;
class Employee {
protected:
string _name;
public:
void work() {
cout << _name << ":work" << endl;
};
};
class TA : public Employee{
public:
TA() {
_name = "TA";
}
};
class Developer : public Employee{
public:
Developer() {
_name = "Developer";
}
};
class Manager : public Employee{
public:
Manager() {
_name = "Manager";
}
};
class EmployeeFactory {
map<string, std::function<Employee*()>> _map;
public:
EmployeeFactory() {
_map.insert({"ta", []() { return (Employee *) new TA(); }});
_map.insert({"manager", []() { return (Employee *) new Manager(); }});
_map.insert({"developer", []() { return (Employee *) new Developer(); }});
}
Employee *create(const string& key) {
if (_map.find(key) != _map.end())
return _map.at(key)();
return nullptr;
}
};
int main() {
EmployeeFactory* employeeFactory = new EmployeeFactory();
Employee *e = employeeFactory->create("developer");
if(e) {
e->work();
delete e;
}
e = employeeFactory->create("ta");
if(e) {
e->work();
delete e;
}
e = employeeFactory->create("garbage collector");
if(e) {
e->work();
delete e;
}
delete employeeFactory;
return 0;
}