设计模式之抽象工厂模式(c++)

问题描述

假设我们要开发一款游戏, 当然为了吸引更多的人玩, 游戏难度不能太大(让大家都没有信心了,估计游戏也就没有前途了),但是也不能太简单(没有挑战性也不符合玩家的心理)。于是我们就可以采用这样一种处理策略: 为游戏设立等级,初级、中级、高级甚至有BT 级。 假设也是过关的游戏, 每个关卡都有一些怪物( monster) 守着, 玩家要把这些怪物干掉才可以过关。 作为开发者, 我们就不得不创建怪物的类, 然后初级怪物、 中级怪物等都继承自怪物类(当然不同种类的则需要另创建类,但是模式相同)。在每个关卡, 我们都要创建怪物的实例,例如初级就创建初级怪物( 有很多种类)、中级创建中级怪物等。可以想象在这个系统中, 将会有成千上万的怪物实例要创建, 问题是还要保证创建的时候不会出错:初级不能创建 BT 级的怪物(玩家就郁闷了,玩家一郁闷,游戏也就挂挂了),反之也不可以。

AbstractFactory 模式就是用来解决这类问题的:要创建一组相关或者相互依赖的对象。

UML类图

其关键就是这一组对象的创建封装到一个用于创建对象的类( ConcreteFactory)中, 维护这样一个创建类总比维护 n 多相关对象的创建过程要简单的多

适用场合

工厂方法模式适用于产品种类结构单一的场合,为一类产品提供创建的接口;而抽象工厂方法适用于产品种类结构多的场合,主要用于创建一组(有多个种类)相关的产品,为它们提供创建的接口;就是当具有多个抽象角色时,抽象工厂便可以派上用场。

代码实现

 

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <string>
using  namespace  std;
 
class  IUser
{
public :
     virtual  void  getUser() = 0;
     virtual  void  setUser() = 0;
};
 
class  SqlUser : public  IUser
{
public :
     void  getUser()
     {
         cout <<  "在sql中返回user"  << endl;
     }
     void  setUser()
     {
         cout <<  "在sql中设置user"  << endl;
     }
};
 
class  AccessUser : public  IUser
{
public :
     void  getUser()
     {
         cout <<  "在Access中返回user"  << endl;
     }
     void  setUser()
     {
         cout <<  "在Access中设置user"  << endl;
     }
};
 
 
class  IDepartment
{
public :
     virtual  void  getDepartment() = 0;
     virtual  void  setDepartment() = 0;
};
 
class  SqlDepartment : public  IDepartment
{
public :
     void  getDepartment()
     {
         cout <<  "在sql中返回Department"  << endl;
     }
     void  setDepartment()
     {
         cout <<  "在sql中设置Department"  << endl;
     }
};
 
class  AccessDepartment : public  IDepartment
{
public :
     void  getDepartment()
     {
         cout <<  "在Access中返回Department"  << endl;
     }
     void  setDepartment()
     {
         cout <<  "在Access中设置Department"  << endl;
     }
};
 
class  IFactory
{
public :
     virtual  IUser *createUser() = 0;
     virtual  IDepartment *createDepartment() = 0;
};
 
class  SqlFactory : public  IFactory
{
public :
     IUser *createUser()
     {
         return  new  SqlUser();
     }
     IDepartment *createDepartment()
     {
         return  new  SqlDepartment();
     }
};
 
class  AccessFactory : public  IFactory
{
public :
     IUser *createUser()
     {
         return  new  AccessUser();
     }
     IDepartment *createDepartment()
     {
         return  new  AccessDepartment();
     }
};
 
/*************************************************************/
 
class  DataAccess
{
private :
     static  string db;
     //string db="access";
public :
     static  IUser *createUser()
     {
         if  (db ==  "access" )
         {
             return  new  AccessUser();
         }
         else  if  (db ==  "sql" )
         {
             return  new  SqlUser();
         }
     }
     static  IDepartment *createDepartment()
     {
         if  (db ==  "access" )
         {
             return  new  AccessDepartment();
         }
         else  if  (db ==  "sql" )
         {
             return  new  SqlDepartment();
         }
     }
};
string DataAccess::db =  "sql" ;
 
/*************************************************************/
 
int  main()
{
     //IFactory *factory=new SqlFactory();
     IFactory *factory;
     IUser *user;
     IDepartment *department;
 
     factory =  new  AccessFactory();
     user = factory->createUser();
     department = factory->createDepartment();
 
     user->getUser();
     user->setUser();
     department->getDepartment();
     department->setDepartment();
 
     user = DataAccess::createUser();
     department = DataAccess::createDepartment();
 
     user->getUser();
     user->setUser();
     department->getDepartment();
     department->setDepartment();
 
     return  0;

运行结果:

 

 

参考文献:

《C++设计模式》

《大话设计模式》

可推荐博客:C++设计模式——抽象工厂模式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值