-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAbstractFactory.cpp
118 lines (94 loc) · 2.94 KB
/
AbstractFactory.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
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
111
112
113
114
115
116
#include <memory>
#include <string>
#include <iostream>
class User{
private:
int _id;
std::string _name;
};
class UserOperation{
public:
virtual void Insert(const User &user) = 0;
virtual User GetUser(int id) = 0;
};
class MongoUserOperation :public UserOperation{
public :
void Insert(const User &user){
std::cout<<"MongoDB: insert user info"<<std::endl;
}
User GetUser(int id){
std::cout<<"MongoDB: select User in id="<<id<<std::endl;
return User();
}
};
class MySQLUserOperation :public UserOperation{
public :
void Insert(const User &user){
std::cout<<"MySQL: insert user info"<<std::endl;
}
User GetUser(int id){
std::cout<<"MySQL: select User in id="<<id<<std::endl;
return User();
}
};
class Department{
private:
int _id;
std::string _depName;
};
class DepartmentOperation{
public:
virtual void Insert(const Department &user) = 0;
virtual Department GetDepartment(int id) = 0;
};
class MongoDepartmentOperation :public DepartmentOperation{
public :
void Insert(const Department &user){
std::cout<<"MongoDB: insert Department info"<<std::endl;
}
Department GetDepartment(int id){
std::cout<<"MongoDB: select Department in id="<<id<<std::endl;
return Department();
}
};
class MySQLDepartmentOperation :public DepartmentOperation{
public :
void Insert(const Department &user){
std::cout<<"MySQL: insert Department info"<<std::endl;
}
Department GetDepartment(int id){
std::cout<<"MySQL: select Department in id="<<id<<std::endl;
return Department();
}
};
class Factory{
public:
virtual std::shared_ptr<UserOperation> createUserOperation() = 0;
virtual std::shared_ptr<DepartmentOperation> createDepartmentOperation() = 0;
};
class MySQLFactory:public Factory{
std::shared_ptr<UserOperation> createUserOperation(){
return std::make_shared<MySQLUserOperation>();
}
std::shared_ptr<DepartmentOperation> createDepartmentOperation(){
return std::make_shared<MySQLDepartmentOperation>();
}
};
class MongoFactory:public Factory{
std::shared_ptr<UserOperation> createUserOperation(){
return std::make_shared<MongoUserOperation>();
}
std::shared_ptr<DepartmentOperation> createDepartmentOperation(){
return std::make_shared<MongoDepartmentOperation>();
}
};
int main(){
std::shared_ptr<Factory> f = std::make_shared<MongoFactory>();
std::shared_ptr<UserOperation> useroperation = f->createUserOperation();
User user = useroperation->GetUser(1);
useroperation->Insert(user);
std::shared_ptr<DepartmentOperation> depoperation = f->createDepartmentOperation();
Department dep = depoperation->GetDepartment(2);
depoperation->Insert(dep);
return 0;
}