抽象工厂模式,就是把工厂类也给他多态了。
根据需求不同的工厂生产不同品种的实体。比如在游戏中。 纯在vip用户对应的vip的宠物对应的vip的怪物,或者vip任务。因此,vip工厂就生产vip这些东西了。但是普通的也要生产。因此 就抽象出来两个类了。
下面我是根据游戏中,不同的角色来定义的一些实体类。直接上图,大家就看明白了
也就是特殊对象特殊对待。因此工厂就要特殊了。也就是要把工厂类抽象出来一个。
下面就上打码了,代码量比较大。。不过自己很快就实现了。
- /************************************************************************/
- /* @filename abstractFactory.cpp
- 04. @author wallwind
- 05. @createtime 2012/10/21 00:40
- 06. @function 抽象工厂模式
- 07. @email wochenglin@qq.com
- 08.*/
- /************************************************************************/
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- //用户类
- class User
- {
- public:
- User (){}
- virtual ~User (){}
- virtual void attack() = 0;
- virtual void defense() = 0;
- };
- //怪物类
- class Monster
- {
- public:
- Monster (){}
- virtual ~Monster (){}
- virtual void attack() = 0;
- virtual void defense() = 0;
- };
- //宠物类
- class Pet
- {
- public:
- Pet (){}
- virtual ~Pet (){}
- virtual void attack() = 0;
- virtual void defense() = 0;
- };
- //游戏中还有很多角色。比如刀客,异人等
- class SwordUser:public User
- {
- public:
- SwordUser(){}
- ~SwordUser(){}
- void attack()
- {
- cout<<"SwordUser:attack"<<endl;
- }
- void defense()
- {
- cout<<"SwordUser:defense"<<endl;
- }
- };
- class SpecialUser:public User
- {
- public:
- SpecialUser(){}
- ~SpecialUser(){}
- void attack()
- {
- cout<<"SpecialUser:attack"<<endl;
- }
- void defense()
- {
- cout<<"SpecialUser:defense"<<endl;
- }
- };
- //////怪物氛围一般的怪物,和BOSS类。
- class GeneralMonster:public Monster
- {
- public:
- GeneralMonster(){}
- ~GeneralMonster(){}
- void attack()
- {
- cout<<"GeneralMonster:attack"<<endl;
- }
- void defense()
- {
- cout<<"GeneralMonster:defense"<<endl;
- }
- };
- class BossMonster:public Monster
- {
- public:
- BossMonster(){}
- ~BossMonster(){}
- void attack()
- {
- cout<<"BossMonster:attack"<<endl;
- }
- void defense()
- {
- cout<<"BossMonster:defense"<<endl;
- }
- };
- ////宠物很多种,比如用来进攻的。还有用来骑得等等
- class AttackPet:public Pet
- {
- public:
- AttackPet(){}
- ~AttackPet(){}
- void attack()
- {
- cout<<"AttackPet:attack"<<endl;
- }
- void defense()
- {
- cout<<"AttackPet:defense"<<endl;
- }
- };
- class GeneralPet:public Pet
- {
- public:
- GeneralPet(){}
- ~GeneralPet(){}
- void attack()
- {
- cout<<"GeneralPet:attack"<<endl;
- }
- void defense()
- {
- cout<<"GeneralPet:defense"<<endl;
- }
- };
- ////抽象工厂类
- class AbstractFactory
- {
- public:
- AbstractFactory(){}
- virtual ~AbstractFactory(){}
- virtual User * createUser() = 0;
- virtual Monster* createMonster() = 0;
- virtual Pet* createPet() = 0;
- };
- class SpecialFactory: public AbstractFactory
- {
- public :
- SpecialFactory(){}
- ~SpecialFactory(){}
- User* createUser()
- {
- User *retUser;
- retUser = new SwordUser();
- return retUser;
- }
- Monster* createMonster()
- {
- Monster* retMonster;
- retMonster = new BossMonster();
- return retMonster;
- }
- Pet* createPet()
- {
- Pet* retPet;
- retPet = new AttackPet();
- return retPet;
- }
- };
- class GeneralFactory: public AbstractFactory
- {
- public :
- GeneralFactory(){}
- ~GeneralFactory(){}
- User* createUser()
- {
- User *retUser;
- retUser = new SpecialUser();
- return retUser;
- }
- Monster* createMonster()
- {
- Monster* retMonster;
- retMonster = new GeneralMonster();
- return retMonster;
- }
- Pet* createPet()
- {
- Pet* retPet;
- retPet = new GeneralPet();
- return retPet;
- }
- };
- class RoleCreate
- {
- public:
- RoleCreate(AbstractFactory* af):m_role(af){}
- ~RoleCreate()
- {
- if (m_role!=NULL)
- {
- delete m_role;
- }
- }
- void factory()
- {
- m_role->createUser()->attack();
- m_role->createMonster()->attack();
- m_role->createPet()->attack();
- }
- private:
- AbstractFactory* m_role;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- AbstractFactory* factory;
- factory= new SpecialFactory();
- RoleCreate role(factory);
- role.factory();
- factory = new GeneralFactory();
- RoleCreate role1(factory);
- role1.factory();
- if (factory!=NULL)
- {
- delete factory;
- factory =NULL;
- }
- return 0;
- }