在游戏开发过程中,我们避免不了创建各种各样的人物职业,及怪物。试想一下,我有几十个大分类,复杂的层次结构,那么这个创建过程怎么维护。
不错,我们可以使用工厂模式进行创建过程,大大简化了维护方面的困难。
我们可以使用简单工厂模式解决此类问题。简单工厂模式,在一定程度上支持了开闭原则,当增加一种职业的时候符合开闭原则
如下:
然而,如果是各种职业分了种族,试想一下,种族几个大分类,职业几个小分类等等,复杂的层次结构,那么这个工厂模式怎么维护。
于是我们用到了抽象工厂模式
如下:
抽象工厂模式与工厂方法模式的区别在抽象工厂模式中,有一个产品族的概念:所谓的产品族,是指位于不同产品等级结构中功能相关联的产品组成的家族。抽象工厂模式所提供的一系列产品就组成一个产品族;而工厂方法提供的一系列产品称为一个等级结构。
开发过程中,同样也会碰到各种怪物,并且各种不同的怪物又分等级(简单,一般,困难,或其它归类供玩家选择适合自己的娱乐)
同样,如果保证创建过程中不出错,不能在简单模式下创建困难的怪物,也不能在困难的模式下创建简单的物怪。
这样的话,游戏就完了。
这里同样我们也可以使用抽象工厂模式
抽象工厂模式的优点
抽象工厂模式除了具有工厂方法模式的优点外,最主要的优点就是可以在类的内部对产品族进行约束。所谓的产品族,一般或多或少的都存在一定的关联,抽象工厂模式就可以在类内部对产品族的关联关系进行定义和描述,而不必专门引入一个新的类来进行管理。
抽象工厂模式的缺点
产品族的扩展将是一件十分费力的事情,假如产品族中需要增加一个新的产品,则几乎所有的工厂类都需要进行修改。所以使用抽象工厂模式时,对产品等级结构的划分是非常重要的。
适用情况
当需要创建的对象是一系列相互关联或相互依赖的产品族时,便可以使用抽象工厂模式。说的更明白一点,就是一个继承体系中,如果存在着多个等级结构(即存在着多个抽象类),并且分属各个等级结构中的实现类之间存在着一定的关联或者约束,就可以使用抽象工厂模式。假如各个等级结构中的实现类之间不存在关联或约束,则使用多个独立的工厂来对产品进行创建,则更合适一点。
运行结果:
代码及下载地址
http://download.csdn.net/download/xiaoting451292510/4766488
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GAME_FACTORY_MonsterAbstractFactory.h
/**
@file GAME_FACTORY_MonsterAbstractFactory.h
@brief the abstract factory of Monster, use to create Monster (different level)
@author arvin
@version 1.0 2012/11/12
*/
#ifndef CXX_GAME_FACTORY_MONSTERABSTRACTFACTORY_H
#define CXX_GAME_FACTORY_MONSTERABSTRACTFACTORY_H
class Monster;
class MonsterAbstractFactory
{
/************************************************************************/
/* function */
public:
/**
* Destruction
*
* @param VOID
* @return
*/
virtual ~MonsterAbstractFactory();
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (different level)
*/
virtual Monster* CreateMonsterA() = 0;
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (different level)
*/
virtual Monster* CreateMonsterB() = 0;
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (different level)
*/
virtual Monster* CreateMonsterC() = 0;
protected:
/**
* Construction
*
* @param VOID
* @return
*/
MonsterAbstractFactory();
private:
/************************************************************************/
/************************************************************************/
/* member */
public:
protected:
private:
/************************************************************************/
};
#endif /* >>CXX_GAME_FACTORY_MONSTERABSTRACTFACTORY_H<< */
/* EOF */
GAME_FACTORY_MonsterFactoryLevel1.h
/**
@file GAME_FACTORY_MonsterFactoryLevel1.h
@brief the level 1 factory of Monster, use to create Monster (level 1)
@author arvin
@version 1.0 2012/11/12
*/
#ifndef CXX_GAME_FACTORY_MONSTERFACTORYLEVEL1_H
#define CXX_GAME_FACTORY_MONSTERFACTORYLEVEL1_H
#ifndef CXX_GAME_FACTORY_MONSTERABSTRACTFACTORY_H
#include "GAME_FACTORY_MonsterAbstractFactory.h"
#endif
class MonsterFactoryLevel1 : public MonsterAbstractFactory
{
/************************************************************************/
/* function */
public:
/**
* Destruction
*
* @param VOID
* @return
*/
virtual ~MonsterFactoryLevel1();
/**
* Instance
*
* @param VOID
* @return MonsterFactoryLevel1*
* @note singleton
*/
static MonsterFactoryLevel1* Instance();
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
static VOID Destroy();
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 1)
*/
virtual Monster* CreateMonsterA();
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 1)
*/
virtual Monster* CreateMonsterB();
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 1)
*/
virtual Monster* CreateMonsterC();
protected:
/**
* Construction
*
* @param VOID
* @return
*/
MonsterFactoryLevel1();
private:
/************************************************************************/
/************************************************************************/
/* member */
public:
protected:
private:
static MonsterFactoryLevel1* m_pInstance;
/************************************************************************/
};
#endif /* >>CXX_GAME_FACTORY_MONSTERFACTORYLEVEL1_H<< */
/* EOF */
GAME_FACTORY_MonsterFactoryLevel2.h
/**
@file GAME_FACTORY_MonsterFactoryLevel1.h
@brief the level 1 factory of Monster, use to create Monster (level 1)
@author arvin
@version 1.0 2012/11/12
*/
#ifndef CXX_GAME_FACTORY_MONSTERFACTORYLEVEL1_H
#define CXX_GAME_FACTORY_MONSTERFACTORYLEVEL1_H
#ifndef CXX_GAME_FACTORY_MONSTERABSTRACTFACTORY_H
#include "GAME_FACTORY_MonsterAbstractFactory.h"
#endif
class MonsterFactoryLevel1 : public MonsterAbstractFactory
{
/************************************************************************/
/* function */
public:
/**
* Destruction
*
* @param VOID
* @return
*/
virtual ~MonsterFactoryLevel1();
/**
* Instance
*
* @param VOID
* @return MonsterFactoryLevel1*
* @note singleton
*/
static MonsterFactoryLevel1* Instance();
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
static VOID Destroy();
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 1)
*/
virtual Monster* CreateMonsterA();
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 1)
*/
virtual Monster* CreateMonsterB();
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 1)
*/
virtual Monster* CreateMonsterC();
protected:
/**
* Construction
*
* @param VOID
* @return
*/
MonsterFactoryLevel1();
private:
/************************************************************************/
/************************************************************************/
/* member */
public:
protected:
private:
static MonsterFactoryLevel1* m_pInstance;
/************************************************************************/
};
#endif /* >>CXX_GAME_FACTORY_MONSTERFACTORYLEVEL1_H<< */
/* EOF */
GAME_FACTORY_MonsterFactoryLevel3.h
/**
@file GAME_FACTORY_MonsterFactoryLevel3.h
@brief the level 3 factory of Monster, use to create Monster (level 3)
@author arvin
@version 1.0 2012/11/12
*/
#ifndef CXX_GAME_FACTORY_MONSTERFACTORYLEVEL3_H
#define CXX_GAME_FACTORY_MONSTERFACTORYLEVEL3_H
#ifndef CXX_GAME_FACTORY_MONSTERABSTRACTFACTORY_H
#include "GAME_FACTORY_MonsterAbstractFactory.h"
#endif
class MonsterFactoryLevel3 : public MonsterAbstractFactory
{
/************************************************************************/
/* function */
public:
/**
* Destruction
*
* @param VOID
* @return
*/
virtual ~MonsterFactoryLevel3();
/**
* Instance
*
* @param VOID
* @return MonsterFactoryLevel3*
* @note singleton
*/
static MonsterFactoryLevel3* Instance();
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
static VOID Destroy();
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 3)
*/
virtual Monster* CreateMonsterA();
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 3)
*/
virtual Monster* CreateMonsterB();
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 3)
*/
virtual Monster* CreateMonsterC();
protected:
/**
* Construction
*
* @param VOID
* @return
*/
MonsterFactoryLevel3();
private:
/************************************************************************/
/************************************************************************/
/* member */
public:
protected:
private:
static MonsterFactoryLevel3* m_pInstance;
/************************************************************************/
};
#endif /* >>CXX_GAME_FACTORY_MONSTERFACTORYLEVEL3_H<< */
/* EOF */
GAME_FACTORY_ProfessionAbstractFactory.h
/**
@file GAME_FACTORY_ProfessionAbstractFactory.h
@brief the abstract factory of profession, use to create the professions (different race)
@author arvin
@version 1.0 2012/11/12
*/
#ifndef CXX_GAME_FACTORY_PROFESSIONABSTRACTFACTORY_H
#define CXX_GAME_FACTORY_PROFESSIONABSTRACTFACTORY_H
class Profession;
class ProfessionAbstractFactory
{
public:
/**
* Destruction
*
* @param VOID
* @return
*/
virtual ~ProfessionAbstractFactory();
/**
* Create Warrior
*
* @param VOID
* @return Profession*
* @note use to create the Warrior (different race)
*/
virtual Profession* CreateWarrior() = 0;
/**
* Create Master
*
* @param VOID
* @return Profession*
* @note use to create the Master (different race)
*/
virtual Profession* CreateMaster() = 0;
/**
* Create Priest
*
* @param VOID
* @return Profession*
* @note use to create the Priest (different race)
*/
virtual Profession* CreatePriest() = 0;
/**
* Create Robber
*
* @param VOID
* @return Profession*
* @note use to create the Robber (different race)
*/
virtual Profession* CreateRobber() = 0;
protected:
/**
* Construction
*
* @param VOID
* @return
*/
ProfessionAbstractFactory();
private:
};
#endif /* >>CXX_GAME_FACTORY_PROFESSIONABSTRACTFACTORY_H<< */
/* EOF */
GAME_FACTORY_ProfessionFactoryHuman.h
/**
@file GAME_FACTORY_ProfessionFactoryHuman.h
@brief the Human factory of profession, use to create the professions (Human)
@author arvin
@version 1.0 2012/11/12
*/
#ifndef CXX_GAME_FACTORY_PROFESSIONFACTORYHUMAN_H
#define CXX_GAME_FACTORY_PROFESSIONFACTORYHUMAN_H
#ifndef CXX_GAME_FACTORY_PROFESSIONABSTRACTFACTORY_H
#include "GAME_FACTORY_ProfessionAbstractFactory.h"
#endif
class ProfessionFactoryHuman : public ProfessionAbstractFactory
{
/************************************************************************/
/* function */
public:
/**
* Destruction
*
* @param VOID
* @return
*/
virtual ~ProfessionFactoryHuman();
/**
* Instance
*
* @param VOID
* @return ProfessionFactoryHuman*
* @note singleton
*/
static ProfessionFactoryHuman* Instance();
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
static VOID Destroy();
/**
* Create Warrior
*
* @param VOID
* @return Profession*
* @note use to create the Warrior (Human)
*/
virtual Profession* CreateWarrior();
/**
* Create Master
*
* @param VOID
* @return Profession*
* @note use to create the Master (Human)
*/
virtual Profession* CreateMaster();
/**
* Create Priest
*
* @param VOID
* @return Profession*
* @note use to create the Priest (Human)
*/
virtual Profession* CreatePriest();
/**
* Create Robber
*
* @param VOID
* @return Profession*
* @note use to create the Robber (Human)
*/
virtual Profession* CreateRobber();
protected:
/**
* Construction
*
* @param VOID
* @return
*/
ProfessionFactoryHuman();
private:
/************************************************************************/
/************************************************************************/
/* member */
public:
protected:
private:
static ProfessionFactoryHuman* m_pInstance;
/************************************************************************/
};
#endif /* >>CXX_GAME_FACTORY_PROFESSIONFACTORYHUMAN_H<< */
/* EOF */
GAME_FACTORY_ProfessionFactoryOrc.h
/**
@file GAME_FACTORY_ProfessionFactoryOrc.h
@brief the Orc factory of profession, use to create the professions (Orc)
@author arvin
@version 1.0 2012/11/12
*/
#ifndef CXX_GAME_FACTORY_PROFESSIONFACTORYORC_H
#define CXX_GAME_FACTORY_PROFESSIONFACTORYORC_H
#ifndef CXX_GAME_FACTORY_PROFESSIONABSTRACTFACTORY_H
#include "GAME_FACTORY_ProfessionAbstractFactory.h"
#endif
class ProfessionFactoryOrc : public ProfessionAbstractFactory
{
/************************************************************************/
/* function */
public:
/**
* Destruction
*
* @param VOID
* @return
*/
virtual ~ProfessionFactoryOrc();
/**
* Instance
*
* @param VOID
* @return ProfessionFactoryOrc*
* @note singleton
*/
static ProfessionFactoryOrc* Instance();
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
static VOID Destroy();
/**
* Create Warrior
*
* @param VOID
* @return Profession*
* @note use to create the Warrior (Orc)
*/
virtual Profession* CreateWarrior();
/**
* Create Master
*
* @param VOID
* @return Profession*
* @note use to create the Master (Orc)
*/
virtual Profession* CreateMaster();
/**
* Create Priest
*
* @param VOID
* @return Profession*
* @note use to create the Priest (Orc)
*/
virtual Profession* CreatePriest();
/**
* Create Robber
*
* @param VOID
* @return Profession*
* @note use to create the Robber (Orc)
*/
virtual Profession* CreateRobber();
protected:
/**
* Construction
*
* @param VOID
* @return
*/
ProfessionFactoryOrc();
private:
/************************************************************************/
/************************************************************************/
/* member */
public:
protected:
private:
static ProfessionFactoryOrc* m_pInstance;
/************************************************************************/
};
#endif /* >>CXX_GAME_FACTORY_PROFESSIONFACTORYORC_H<< */
/* EOF */
GAME_FACTORY_ProfessionFactoryUndead.h
/**
@file GAME_FACTORY_ProfessionFactoryUndead.h
@brief the Undead factory of profession, use to create the professions (Undead)
@author arvin
@version 1.0 2012/11/12
*/
#ifndef CXX_GAME_FACTORY_PROFESSIONFACTORYUNDEAD_H
#define CXX_GAME_FACTORY_PROFESSIONFACTORYUNDEAD_H
#ifndef CXX_GAME_FACTORY_PROFESSIONABSTRACTFACTORY_H
#include "GAME_FACTORY_ProfessionAbstractFactory.h"
#endif
class ProfessionFactoryUndead : public ProfessionAbstractFactory
{
/************************************************************************/
/* function */
public:
/**
* Destruction
*
* @param VOID
* @return
*/
virtual ~ProfessionFactoryUndead();
/**
* Instance
*
* @param VOID
* @return ProfessionFactoryUndead*
* @note singleton
*/
static ProfessionFactoryUndead* Instance();
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
static VOID Destroy();
/**
* Create Warrior
*
* @param VOID
* @return Profession*
* @note use to create the Warrior (Undead)
*/
virtual Profession* CreateWarrior();
/**
* Create Master
*
* @param VOID
* @return Profession*
* @note use to create the Master (Undead)
*/
virtual Profession* CreateMaster();
/**
* Create Priest
*
* @param VOID
* @return Profession*
* @note use to create the Priest (Undead)
*/
virtual Profession* CreatePriest();
/**
* Create Robber
*
* @param VOID
* @return Profession*
* @note use to create the Robber (Undead)
*/
virtual Profession* CreateRobber();
protected:
/**
* Construction
*
* @param VOID
* @return
*/
ProfessionFactoryUndead();
private:
/************************************************************************/
/************************************************************************/
/* member */
public:
protected:
private:
static ProfessionFactoryUndead* m_pInstance;
/************************************************************************/
};
#endif /* >>CXX_GAME_FACTORY_PROFESSIONFACTORYUNDEAD_H<< */
/* EOF */
GAME_FACTORY_MonsterAbstractFactory.cpp
/**
@file GAME_FACTORY_MonsterAbstractFactory.h
@brief the abstract factory of Monster, use to create Monster (different level)
@author arvin
@version 1.0 2012/11/12
*/
#include "stdafx.h"
#ifndef CXX_GAME_FACTORY_MONSTERABSTRACTFACTORY_H
#include "GAME_FACTORY_MonsterAbstractFactory.h"
#endif
/**
* Construction
*
* @param VOID
* @return
*/
MonsterAbstractFactory::MonsterAbstractFactory()
{
}
/**
* Destruction
*
* @param VOID
* @return
*/
MonsterAbstractFactory::~MonsterAbstractFactory()
{
}
/* EOF */
GAME_FACTORY_MonsterFactoryLevel1.cpp
/**
@file GAME_FACTORY_MonsterFactoryLevel1.cpp
@brief the level 1 factory of Monster, use to create Monster (level 1)
@author arvin
@version 1.0 2012/11/12
*/
#include "stdafx.h"
#ifndef CXX_GAME_FACTORY_MONSTERFACTORYLEVEL1_H
#include "GAME_FACTORY_MonsterFactoryLevel1.h"
#endif
MonsterFactoryLevel1* MonsterFactoryLevel1::m_pInstance = NULL;
/**
* Construction
*
* @param VOID
* @return
*/
MonsterFactoryLevel1::MonsterFactoryLevel1()
{
}
/**
* Destruction
*
* @param VOID
* @return
*/
MonsterFactoryLevel1::~MonsterFactoryLevel1()
{
}
/**
* Instance
*
* @param VOID
* @return MonsterFactoryLevel1*
* @note singleton
*/
MonsterFactoryLevel1*
MonsterFactoryLevel1::Instance()
{
if (NULL == m_pInstance) {
m_pInstance = new MonsterFactoryLevel1;
}
return m_pInstance;
}
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
VOID
MonsterFactoryLevel1::Destroy()
{
if (NULL != m_pInstance) {
delete m_pInstance;
m_pInstance = NULL;
}
}
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 1)
*/
Monster*
MonsterFactoryLevel1::CreateMonsterA()
{
return NULL;
}
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 1)
*/
Monster*
MonsterFactoryLevel1::CreateMonsterB()
{
return NULL;
}
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 1)
*/
Monster*
MonsterFactoryLevel1::CreateMonsterC()
{
return NULL;
}
/* EOF */
GAME_FACTORY_MonsterFactoryLevel2.cpp
/**
@file GAME_FACTORY_MonsterFactoryLevel2.cpp
@brief the level 2 factory of Monster, use to create Monster (level 2)
@author arvin
@version 1.0 2012/11/12
*/
#include "stdafx.h"
#ifndef CXX_GAME_FACTORY_MONSTERFACTORYLEVEL2_H
#include "GAME_FACTORY_MonsterFactoryLevel2.h"
#endif
MonsterFactoryLevel2* MonsterFactoryLevel2::m_pInstance = NULL;
/**
* Construction
*
* @param VOID
* @return
*/
MonsterFactoryLevel2::MonsterFactoryLevel2()
{
}
/**
* Destruction
*
* @param VOID
* @return
*/
MonsterFactoryLevel2::~MonsterFactoryLevel2()
{
}
/**
* Instance
*
* @param VOID
* @return MonsterFactoryLevel2*
* @note singleton
*/
MonsterFactoryLevel2*
MonsterFactoryLevel2::Instance()
{
if (NULL == m_pInstance) {
m_pInstance = new MonsterFactoryLevel2;
}
return m_pInstance;
}
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
VOID
MonsterFactoryLevel2::Destroy()
{
if (NULL != m_pInstance) {
delete m_pInstance;
m_pInstance = NULL;
}
}
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 2)
*/
Monster*
MonsterFactoryLevel2::CreateMonsterA()
{
return NULL;
}
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 2)
*/
Monster*
MonsterFactoryLevel2::CreateMonsterB()
{
return NULL;
}
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 2)
*/
Monster*
MonsterFactoryLevel2::CreateMonsterC()
{
return NULL;
}
/* EOF */
GAME_FACTORY_MonsterFactoryLevel3.cpp
/**
@file GAME_FACTORY_MonsterFactoryLevel3.cpp
@brief the level 3 factory of Monster, use to create Monster (level 3)
@author arvin
@version 1.0 2012/11/12
*/
#include "stdafx.h"
#ifndef CXX_GAME_FACTORY_MONSTERFACTORYLEVEL3_H
#include "GAME_FACTORY_MonsterFactoryLevel3.h"
#endif
MonsterFactoryLevel3* MonsterFactoryLevel3::m_pInstance = NULL;
/**
* Construction
*
* @param VOID
* @return
*/
MonsterFactoryLevel3::MonsterFactoryLevel3()
{
}
/**
* Destruction
*
* @param VOID
* @return
*/
MonsterFactoryLevel3::~MonsterFactoryLevel3()
{
}
/**
* Instance
*
* @param VOID
* @return MonsterFactoryLevel3*
* @note singleton
*/
MonsterFactoryLevel3*
MonsterFactoryLevel3::Instance()
{
if (NULL == m_pInstance) {
m_pInstance = new MonsterFactoryLevel3;
}
return m_pInstance;
}
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
VOID
MonsterFactoryLevel3::Destroy()
{
if (NULL != m_pInstance) {
delete m_pInstance;
m_pInstance = NULL;
}
}
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 3)
*/
Monster*
MonsterFactoryLevel3::CreateMonsterA()
{
return NULL;
}
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 3)
*/
Monster*
MonsterFactoryLevel3::CreateMonsterB()
{
return NULL;
}
/**
* Create Monster
*
* @param VOID
* @return Monster*
* @use to create Monster (level 3)
*/
Monster*
MonsterFactoryLevel3::CreateMonsterC()
{
return NULL;
}
/* EOF */
GAME_FACTORY_ProfessionAbstractFactory.cpp
/**
@file GAME_FACTORY_ProfessionAbstractFactory.cpp
@brief the abstract factory of profession, use to create the professions (different race)
@author arvin
@version 1.0 2012/11/12
*/
#include "stdafx.h"
#ifndef CXX_GAME_FACTORY_PROFESSIONABSTRACTFACTORY_H
#include "GAME_FACTORY_ProfessionAbstractFactory.h"
#endif
/**
* Construction
*
* @param VOID
* @return
*/
ProfessionAbstractFactory::ProfessionAbstractFactory()
{
}
/**
* Destruction
*
* @param VOID
* @return
*/
ProfessionAbstractFactory::~ProfessionAbstractFactory()
{
}
/* EOF */
GAME_FACTORY_ProfessionFactoryHuman.cpp
/**
@file GAME_FACTORY_ProfessionFactoryHuman.cpp
@brief the Human factory of profession, use to create the professions (Human)
@author arvin
@version 1.0 2012/11/12
*/
#include "stdafx.h"
#ifndef CXX_GAME_FACTORY_PROFESSIONFACTORYHUMAN_H
#include "GAME_FACTORY_ProfessionFactoryHuman.h"
#endif
ProfessionFactoryHuman* ProfessionFactoryHuman::m_pInstance = NULL;
/**
* Construction
*
* @param VOID
* @return
*/
ProfessionFactoryHuman::ProfessionFactoryHuman()
{
}
/**
* Destruction
*
* @param VOID
* @return
*/
ProfessionFactoryHuman::~ProfessionFactoryHuman()
{
}
/**
* Instance
*
* @param VOID
* @return ProfessionFactoryHuman*
* @note singleton
*/
ProfessionFactoryHuman*
ProfessionFactoryHuman::Instance()
{
if (NULL == m_pInstance) {
m_pInstance = new ProfessionFactoryHuman;
}
return m_pInstance;
}
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
VOID
ProfessionFactoryHuman::Destroy()
{
if (NULL != m_pInstance) {
delete m_pInstance;
m_pInstance = NULL;
}
}
/**
* Create Warrior
*
* @param VOID
* @return Profession*
* @note use to create the Warrior (Human)
*/
Profession*
ProfessionFactoryHuman::CreateWarrior()
{
return NULL;
}
/**
* Create Master
*
* @param VOID
* @return Profession*
* @note use to create the Master (Human)
*/
Profession*
ProfessionFactoryHuman::CreateMaster()
{
return NULL;
}
/**
* Create Priest
*
* @param VOID
* @return Profession*
* @note use to create the Priest (Human)
*/
Profession*
ProfessionFactoryHuman::CreatePriest()
{
return NULL;
}
/**
* Create Robber
*
* @param VOID
* @return Profession*
* @note use to create the Robber (Human)
*/
Profession*
ProfessionFactoryHuman::CreateRobber()
{
return NULL;
}
/* EOF */
GAME_FACTORY_ProfessionFactoryOrc.cpp
/**
@file GAME_FACTORY_ProfessionFactoryOrc.cpp
@brief the Orc factory of profession, use to create the professions (Orc)
@author arvin
@version 1.0 2012/11/12
*/
#include "stdafx.h"
#ifndef CXX_GAME_FACTORY_PROFESSIONFACTORYORC_H
#include "GAME_FACTORY_ProfessionFactoryOrc.h"
#endif
ProfessionFactoryOrc* ProfessionFactoryOrc::m_pInstance = NULL;
/**
* Construction
*
* @param VOID
* @return
*/
ProfessionFactoryOrc::ProfessionFactoryOrc()
{
}
/**
* Destruction
*
* @param VOID
* @return
*/
ProfessionFactoryOrc::~ProfessionFactoryOrc()
{
}
/**
* Instance
*
* @param VOID
* @return ProfessionFactoryOrc*
* @note singleton
*/
ProfessionFactoryOrc*
ProfessionFactoryOrc::Instance()
{
if (NULL == m_pInstance) {
m_pInstance = new ProfessionFactoryOrc;
}
return m_pInstance;
}
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
VOID
ProfessionFactoryOrc::Destroy()
{
if (NULL != m_pInstance) {
delete m_pInstance;
m_pInstance = NULL;
}
}
/**
* Create Warrior
*
* @param VOID
* @return Profession*
* @note use to create the Warrior (Orc)
*/
Profession*
ProfessionFactoryOrc::CreateWarrior()
{
return NULL;
}
/**
* Create Master
*
* @param VOID
* @return Profession*
* @note use to create the Master (Orc)
*/
Profession*
ProfessionFactoryOrc::CreateMaster()
{
return NULL;
}
/**
* Create Priest
*
* @param VOID
* @return Profession*
* @note use to create the Priest (Orc)
*/
Profession*
ProfessionFactoryOrc::CreatePriest()
{
return NULL;
}
/**
* Create Robber
*
* @param VOID
* @return Profession*
* @note use to create the Robber (Orc)
*/
Profession*
ProfessionFactoryOrc::CreateRobber()
{
return NULL;
}
/* EOF */
GAME_FACTORY_ProfessionFactoryUndead.cpp
/**
@file GAME_FACTORY_ProfessionFactoryUndead.cpp
@brief the Undead factory of profession, use to create the professions (Undead)
@author arvin
@version 1.0 2012/11/12
*/
#include "stdafx.h"
#ifndef CXX_GAME_FACTORY_PROFESSIONFACTORYUNDEAD_H
#include "GAME_FACTORY_ProfessionFactoryUndead.h"
#endif
ProfessionFactoryUndead* ProfessionFactoryUndead::m_pInstance = NULL;
/**
* Construction
*
* @param VOID
* @return
*/
ProfessionFactoryUndead::ProfessionFactoryUndead()
{
}
/**
* Destruction
*
* @param VOID
* @return
*/
ProfessionFactoryUndead::~ProfessionFactoryUndead()
{
}
/**
* Instance
*
* @param VOID
* @return ProfessionFactoryUndead*
* @note singleton
*/
ProfessionFactoryUndead*
ProfessionFactoryUndead::Instance()
{
if (NULL == m_pInstance) {
m_pInstance = new ProfessionFactoryUndead;
}
return m_pInstance;
}
/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
VOID
ProfessionFactoryUndead::Destroy()
{
if (NULL != m_pInstance) {
delete m_pInstance;
m_pInstance = NULL;
}
}
/**
* Create Warrior
*
* @param VOID
* @return Profession*
* @note use to create the Warrior (Undead)
*/
Profession*
ProfessionFactoryUndead::CreateWarrior()
{
return NULL;
}
/**
* Create Master
*
* @param VOID
* @return Profession*
* @note use to create the Master (Undead)
*/
Profession*
ProfessionFactoryUndead::CreateMaster()
{
return NULL;
}
/**
* Create Priest
*
* @param VOID
* @return Profession*
* @note use to create the Priest (Undead)
*/
Profession*
ProfessionFactoryUndead::CreatePriest()
{
return NULL;
}
/**
* Create Robber
*
* @param VOID
* @return Profession*
* @note use to create the Robber (Undead)
*/
Profession*
ProfessionFactoryUndead::CreateRobber()
{
return NULL;
}
/* EOF */
GameFactory.cpp (Main文件)
// GameFactory.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#ifndef CXX_GAME_FACTORY_MONSTERFACTORYLEVEL1_H
#include "GAME_FACTORY_MonsterFactoryLevel1.h"
#endif
#ifndef CXX_GAME_FACTORY_MONSTERFACTORYLEVEL2_H
#include "GAME_FACTORY_MonsterFactoryLevel2.h"
#endif
#ifndef CXX_GAME_FACTORY_MONSTERFACTORYLEVEL3_H
#include "GAME_FACTORY_MonsterFactoryLevel3.h"
#endif
#ifndef CXX_GAME_FACTORY_PROFESSIONFACTORYHUMAN_H
#include "GAME_FACTORY_ProfessionFactoryHuman.h"
#endif
#ifndef CXX_GAME_FACTORY_PROFESSIONFACTORYORC_H
#include "GAME_FACTORY_ProfessionFactoryOrc.h"
#endif
#ifndef CXX_GAME_FACTORY_PROFESSIONFACTORYUNDEAD_H
#include "GAME_FACTORY_ProfessionFactoryUndead.h"
#endif
MonsterAbstractFactory*
GetMonsterFactory()
{
//Monster Factory
static int i = 0;
i++;
if (3 == i) {
i = 0;
}
if (0 == i) {
//level1
return MonsterFactoryLevel1::Instance();
}
else if (1 == i) {
//level2
return MonsterFactoryLevel2::Instance();
}
else if (2 == i) {
//level3
return MonsterFactoryLevel3::Instance();
}
else{
return NULL;
}
}
ProfessionAbstractFactory*
GetProfessionFactory()
{
//Profession Factory
static int i = 0;
i++;
if (3 == i) {
i = 0;
}
if (0 == i) {
//level1
return ProfessionFactoryHuman::Instance();
}
else if (1 == i) {
//level2
return ProfessionFactoryOrc::Instance();
}
else if (2 == i) {
//level3
return ProfessionFactoryUndead::Instance();
}
else{
return NULL;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//Monster
Monster* pMonsterTemp = NULL;
for (int i=0; i<3; i++)
{
MonsterAbstractFactory* pMonsterFactory = GetMonsterFactory();
if (NULL == pMonsterFactory) {
//error do something
return 0;
}
pMonsterTemp = pMonsterFactory->CreateMonsterA();
pMonsterTemp = pMonsterFactory->CreateMonsterB();
pMonsterTemp = pMonsterFactory->CreateMonsterC();
}
//Profession
Profession* pProfessionTemp = NULL;
for (int i=0; i<3; i++)
{
ProfessionAbstractFactory* pProfessionFactory = GetProfessionFactory();
if (NULL == pProfessionFactory) {
//error do something
return 0;
}
pProfessionTemp = pProfessionFactory->CreateWarrior();
pProfessionTemp = pProfessionFactory->CreateMaster();
pProfessionTemp = pProfessionFactory->CreatePriest();
pProfessionTemp = pProfessionFactory->CreateRobber();
}
return 0;
}