作者:happy可乐可爱多_376_874 | 来源:互联网 | 2024-11-27 13:21
抽象工厂模式包含如下角色:AbstractFactory:抽象工厂ConcreteFactory:具体工厂AbstractProduct:抽象产品Product:具体产品https
抽象工厂模式包含如下角色:
- AbstractFactory:抽象工厂
- ConcreteFactory:具体工厂
- AbstractProduct:抽象产品
- Product:具体产品
1 #include<string>
2 #include
3
4
5
6 //数据库表项:User 与工厂模式无关
7 class User
8 {
9 private:
10 int id;
11 std::string name;
12 public:
13 int getID()
14 {
15 return id;
16 }
17 std::string getName()
18 {
19 return name;
20 }
21 void setID(int ID)
22 {
23 this->id = ID;
24 }
25 void setName(std::string NAME)
26 {
27 this->name = NAME;
28 }
29 };
30
31
32 //数据库表项:Department 与工厂模式无关
33 class Department
34 {
35 private:
36 int id;
37 std::string name;
38 public:
39 int getID()
40 {
41 return id;
42 }
43 std::string getName()
44 {
45 return name;
46 }
47 void setID(int ID)
48 {
49 this->id = ID;
50 }
51 void setName(std::string NAME)
52 {
53 this->name = NAME;
54 }
55 };
56
57
58
59
60 //抽象产品A:IUser
61 class AbstractProductA{
62 public:
63 virtual void Insert(User user) = 0;
64 virtual User* GetUser(int id) = 0;
65 };
66
67 //具体产品A1 SqlProductA
68 class SqlProductA :public AbstractProductA{
69 public:
70 void Insert(User user){
71 std::cout <<"在SQL Server中给User表增加了一条记录" << std::endl;
72 }
73 User* GetUser(int id){
74 std::cout <<"在SQL Server中得到id为:"<" User表一条记录" << std::endl;
75 return NULL;
76 }
77 };
78 //具体产品A2 AccessProductA
79 class AccessProductA :public AbstractProductA{
80 public:
81 void Insert(User user){
82 std::cout <<"在Access中给User表增加了一条记录" << std::endl;
83 }
84 User* GetUser(int id){
85 std::cout <<"在Access中得到id为:" <" User表一条记录" << std::endl;
86 return NULL;
87 }
88 };
89
90 //抽象产品B:IDepartment
91 class AbstractProductB{
92 public:
93 virtual void Insert(Department department) = 0;
94 virtual Department* GetDepartment(int id) = 0;
95 };
96
97
98 //具体产品B1 SqlProductB
99 class SqlProductB :public AbstractProductB{
100 public:
101 void Insert(Department department){
102 std::cout <<"在SQL Server中给department表增加了一条记录" << std::endl;
103 }
104 Department* GetDepartment(int id){
105 std::cout <<"在SQL Server中得到id为:" <" Department表一条记录" << std::endl;
106 return NULL;
107 }
108 };
109
110
111 //具体产品B2 AccessProductB
112 class AccessProductB :public AbstractProductB{
113 public:
114 void Insert(Department department){
115 std::cout <<"在Access中给department表增加了一条记录" << std::endl;
116 }
117 Department* GetDepartment(int id){
118 std::cout <<"在Access中得到id为:" <" Department表一条记录" << std::endl;
119 return NULL;
120 }
121 };
122
123
124 class AbstractFactory{
125 public:
126 virtual AbstractProductA* createProductA() = 0;
127 virtual AbstractProductB* createProductB() = 0;
128 };
129
130 //具体工厂1:SqlServerFactory
131 class SqlFactory :public AbstractFactory
132 {
133 public:
134 AbstractProductA* createProductA()
135 {
136 return new SqlProductA;
137 }
138 AbstractProductB* createProductB()
139 {
140 return new SqlProductB;
141 }
142 };
143
144 //具体工厂2:AccessFactory
145 class AccessFactory :public AbstractFactory{
146 public:
147 AbstractProductA* createProductA()
148 {
149 return new AccessProductA;
150 }
151 AbstractProductB* createProductB()
152 {
153 return new AccessProductB;
154
155 }
156 };
157
158
159 void main()
160 {
161 //初始化数据库
162 User user;
163 Department department;
164
165 //ConcreteFactory1
166 AbstractFactory* factory = NULL;
167 //factory = new SqlFactory;
168 factory = new AccessFactory;
169 //ProductA1
170 AbstractProductA* iu = NULL;
171 iu = factory->createProductA();
172 iu->Insert(user);
173 iu->GetUser(1);
174
175 //ProductB1
176 AbstractProductB* id = NULL;
177 id = factory->createProductB();
178 id->Insert(department);
179 id->GetDepartment(1);
180
181 if (factory != NULL)
182 {
183 delete factory;
184 factory = NULL;
185 }
186 if (iu != NULL)
187 {
188 delete iu;
189 iu = NULL;
190 }
191 if (id != NULL)
192 {
193 delete id;
194 id = NULL;
195 }
196
197 system("pause");
198
199
200
201 }
https://design-patterns.readthedocs.io/zh_CN/latest/creational_patterns/abstract_factory.html
https://blog.csdn.net/xiqingnian/article/details/41181995