《抽象工厂模式》

抽象工厂模式,就是把工厂类也给他多态了。

 

根据需求不同的工厂生产不同品种的实体。比如在游戏中。 纯在vip用户对应的vip的宠物对应的vip的怪物,或者vip任务。因此,vip工厂就生产vip这些东西了。但是普通的也要生产。因此 就抽象出来两个类了。

 

下面我是根据游戏中,不同的角色来定义的一些实体类。直接上图,大家就看明白了

 

 

也就是特殊对象特殊对待。因此工厂就要特殊了。也就是要把工厂类抽象出来一个。

 

下面就上打码了,代码量比较大。。不过自己很快就实现了。

[cpp]  view plain copy
  1. /************************************************************************/    
  2. /* @filename    abstractFactory.cpp  
  3.    04.   @author       wallwind  
  4.    05.   @createtime    2012/10/21 00:40 
  5.    06.   @function      抽象工厂模式  
  6.    07.   @email       wochenglin@qq.com  
  7.    08.*/    
  8.    /************************************************************************/    
  9.   
  10.   
  11. #include "stdafx.h"  
  12. #include <iostream>  
  13.   
  14. using namespace std;  
  15.   
  16. //用户类  
  17. class User  
  18. {  
  19. public:  
  20.      User (){}  
  21.     virtual ~User (){}  
  22.   
  23.     virtual void attack() = 0;  
  24.   
  25.     virtual void defense() = 0;  
  26.   
  27. };  
  28. //怪物类  
  29. class Monster  
  30. {  
  31. public:  
  32.      Monster (){}  
  33.     virtual ~Monster (){}  
  34.   
  35.     virtual void attack() = 0;  
  36.   
  37.     virtual void defense() = 0;  
  38.   
  39. };  
  40. //宠物类  
  41. class Pet  
  42. {  
  43. public:  
  44.      Pet (){}  
  45.     virtual ~Pet (){}  
  46.   
  47.     virtual void attack() = 0;  
  48.   
  49.     virtual void defense() = 0;  
  50.   
  51. };  
  52.   
  53.   
  54. //游戏中还有很多角色。比如刀客,异人等  
  55.   
  56. class SwordUser:public User  
  57. {  
  58. public:  
  59.   
  60.     SwordUser(){}  
  61.     ~SwordUser(){}  
  62.   
  63.     void attack()  
  64.     {  
  65.         cout<<"SwordUser:attack"<<endl;  
  66.     }  
  67.       
  68.     void defense()  
  69.     {  
  70.         cout<<"SwordUser:defense"<<endl;  
  71.     }  
  72.   
  73. };  
  74.   
  75. class SpecialUser:public User  
  76. {  
  77. public:  
  78.   
  79.     SpecialUser(){}  
  80.     ~SpecialUser(){}  
  81.   
  82.     void attack()  
  83.     {  
  84.         cout<<"SpecialUser:attack"<<endl;  
  85.     }  
  86.   
  87.     void defense()  
  88.     {  
  89.         cout<<"SpecialUser:defense"<<endl;  
  90.     }  
  91.   
  92. };  
  93. //怪物氛围一般的怪物,和BOSS类。  
  94.   
  95. class GeneralMonster:public Monster  
  96. {  
  97. public:  
  98.   
  99.     GeneralMonster(){}  
  100.     ~GeneralMonster(){}  
  101.   
  102.     void attack()  
  103.     {  
  104.         cout<<"GeneralMonster:attack"<<endl;  
  105.     }  
  106.   
  107.     void defense()  
  108.     {  
  109.         cout<<"GeneralMonster:defense"<<endl;  
  110.     }  
  111.   
  112. };  
  113.   
  114. class BossMonster:public Monster  
  115. {  
  116. public:  
  117.   
  118.     BossMonster(){}  
  119.     ~BossMonster(){}  
  120.   
  121.     void attack()  
  122.     {  
  123.         cout<<"BossMonster:attack"<<endl;  
  124.     }  
  125.   
  126.     void defense()  
  127.     {  
  128.         cout<<"BossMonster:defense"<<endl;  
  129.     }  
  130.   
  131. };  
  132.   
  133. 宠物很多种,比如用来进攻的。还有用来骑得等等  
  134. class AttackPet:public Pet  
  135. {  
  136. public:  
  137.   
  138.     AttackPet(){}  
  139.     ~AttackPet(){}  
  140.   
  141.     void attack()  
  142.     {  
  143.         cout<<"AttackPet:attack"<<endl;  
  144.     }  
  145.   
  146.     void defense()  
  147.     {  
  148.         cout<<"AttackPet:defense"<<endl;  
  149.     }  
  150.   
  151. };  
  152.   
  153. class GeneralPet:public Pet  
  154. {  
  155. public:  
  156.   
  157.     GeneralPet(){}  
  158.     ~GeneralPet(){}  
  159.   
  160.     void attack()  
  161.     {  
  162.         cout<<"GeneralPet:attack"<<endl;  
  163.     }  
  164.   
  165.     void defense()  
  166.     {  
  167.         cout<<"GeneralPet:defense"<<endl;  
  168.     }  
  169.   
  170. };  
  171.   
  172. 抽象工厂类  
  173.   
  174. class AbstractFactory  
  175. {  
  176. public:  
  177.     AbstractFactory(){}  
  178.     virtual ~AbstractFactory(){}  
  179.   
  180.     virtual User * createUser() = 0;  
  181.     virtual Monster* createMonster() = 0;  
  182.     virtual Pet* createPet() = 0;  
  183. };  
  184.   
  185. class SpecialFactory: public AbstractFactory  
  186. {  
  187.     public :  
  188.         SpecialFactory(){}  
  189.         ~SpecialFactory(){}  
  190.     User* createUser()  
  191.     {  
  192.         User *retUser;  
  193.         retUser = new SwordUser();  
  194.         return retUser;  
  195.     }  
  196.   
  197.     Monster* createMonster()  
  198.     {  
  199.         Monster* retMonster;  
  200.         retMonster = new BossMonster();  
  201.         return retMonster;  
  202.   
  203.     }  
  204.   
  205.     Pet* createPet()  
  206.     {  
  207.         Pet* retPet;  
  208.         retPet = new AttackPet();  
  209.         return retPet;  
  210.     }  
  211.       
  212. };  
  213.   
  214.   
  215. class GeneralFactory: public AbstractFactory  
  216. {  
  217. public :  
  218.     GeneralFactory(){}  
  219.     ~GeneralFactory(){}  
  220.     User* createUser()  
  221.     {  
  222.         User *retUser;  
  223.         retUser = new SpecialUser();  
  224.         return retUser;  
  225.     }  
  226.   
  227.     Monster* createMonster()  
  228.     {  
  229.         Monster* retMonster;  
  230.         retMonster = new GeneralMonster();  
  231.         return retMonster;  
  232.   
  233.     }  
  234.   
  235.     Pet* createPet()  
  236.     {  
  237.         Pet* retPet;  
  238.         retPet = new GeneralPet();  
  239.         return retPet;  
  240.     }  
  241.   
  242. };  
  243.   
  244. class  RoleCreate  
  245. {  
  246. public:  
  247.     RoleCreate(AbstractFactory* af):m_role(af){}  
  248.     ~RoleCreate()  
  249.     {  
  250.         if (m_role!=NULL)  
  251.         {  
  252.             delete m_role;  
  253.         }  
  254.     }  
  255.     void factory()  
  256.     {  
  257.         m_role->createUser()->attack();  
  258.         m_role->createMonster()->attack();  
  259.         m_role->createPet()->attack();  
  260.     }  
  261. private:  
  262.     AbstractFactory* m_role;  
  263.       
  264. };  
  265. int _tmain(int argc, _TCHAR* argv[])  
  266. {  
  267.       
  268.     AbstractFactory* factory;  
  269.   
  270.         factory= new SpecialFactory();  
  271.     RoleCreate role(factory);  
  272.     role.factory();  
  273.   
  274.     factory = new GeneralFactory();  
  275.     RoleCreate role1(factory);  
  276.     role1.factory();  
  277.   
  278.     if (factory!=NULL)  
  279.     {  
  280.         delete factory;  
  281.         factory =NULL;  
  282.     }  
  283.     return 0;  
  284. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值