您的位置:首页 > 移动开发 > Cocos引擎

设计模式在cocos2d-x中的使用--简单工厂模式(Simple Factory)

2014-06-29 15:40 549 查看
什么是简单工厂模式?

从设计模式的类型上来说,简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。

简单工厂模式在cocos2d-x中怎么用,我们通过下面的小例子来了解一下。

假如我们在开发一款类似魔兽的RPG游戏,在游戏中会出现很多种族的角色,如:人族、兽族。

这些种族一般都会定义为一个类,如果兽族Orc类,人族Human类。

兽族、人族两个类都同样属于种族,那么我们可以给他们定义一个共同的父类, 名字叫种族Race类。

既然是种族,肯定有种族的名字,还有长什么样子,我们想把名字和样子显示在屏幕上显示出来,那么我们在父类Race中定义两个属性,一个名字name, 一个形象features。

//
//  Race.h
//  DesignPattern_Factory
//
//  Created by cc on 14-6-28.
//
//

#ifndef __DesignPattern_Factory__Race__
#define __DesignPattern_Factory__Race__

using namespace std;

#include "cocos2d.h"
#include "IRaceConst.h"
#include <string>

class Race : public cocos2d::CCSprite {

protected:

//种族名字
std::string m_name;

//种族形象(用图片表示)
std::string m_features;

public:

#pragma mark  <getters && setter>
//
// 	std::string Name() const { return m_name; }
// 	void Name(std::string val) { m_name = val; }
//
// 	std::string Features() const { return m_features; }
// 	void Features(std::string val) { m_features = val; }

#pragma mark  <构造 && 析构>

/**
*	@brief	构造
*
*/
Race();

/**
*	@brief	析构
*
*/
virtual ~Race();

};

#endif /* defined(__DesignPattern_Factory__Race__) */
父类“种族类”写好了,我们就可以实现人族,兽族和亡灵族三个子类了。

兽族类:

//
//  Orc.h
//  DesignPattern_Factory
//
//  Created by cc on 14-6-28.
//
//

#ifndef __DesignPattern_Factory__Orc__
#define __DesignPattern_Factory__Orc__

#include "Race.h"

class Orc : public Race {

public:

#pragma mark  <构造 && 析构>

/**
*	@brief	构造
*
*/
Orc();

/**
*	@brief	析构
*
*/
virtual ~Orc();

#pragma mark  <创建 && 初始化>

/**
*	@brief	创建兽族
*
*	@return	兽族
*/
static Orc* create();

/**
*	@brief	初始化兽族
*
*	@return	true: 初始化成功 false: 初始化失败
*/
bool init();

};

#endif /* defined(__DesignPattern_Factory__Orc__) */
<pre name="code" class="cpp">//
//  Orc.cpp
//  DesignPattern_Factory
//
//  Created by cc on 14-6-28.
//
//

#include "Orc.h"

#pragma mark  <构造 && 析构>

/**
*	@brief	构造
*
*/
Orc::~Orc() {

}

/**
*	@brief	析构
*
*/
Orc::Orc() {

}

#pragma mark  <创建 && 初始化>

/**
*	@brief	创建兽族
*
*	@return	兽族
*/
Orc* Orc::create() {

Orc *pRet = new Orc();
if (pRet && pRet->init()) {
pRet->autorelease();
return pRet;
}

CC_SAFE_DELETE(pRet);
return NULL;

}

/**
*	@brief	初始化兽族
*
*	@return	true: 初始化成功 false: 初始化失败
*/
bool Orc::init(){

this->m_features = "orc.png";
this->m_name = "兽族";

if (initWithFile(this->m_features.c_str())) {

CCLabelTTF* pLabName = CCLabelTTF::create(this->m_name.c_str(), "Marker Felt", 22.0f);
pLabName->setPosition(ccp(this->getContentSize().width / 2, this->getContentSize().height + 30.0f));
this->addChild(pLabName, 1);

return true;
}

return false;
}


人族类:
//
//  Human.h
//  DesignPattern_Factory
//
//  Created by cc on 14-6-28.
//
//

#ifndef __DesignPattern_Factory__Human__
#define __DesignPattern_Factory__Human__

#include "Race.h"

USING_NS_CC;

class Human : public Race {

public:

#pragma mark  <构造 && 析构>

/**
*	@brief	构造
*
*/
Human();

/**
*	@brief	析构
*
*/
virtual ~Human();

#pragma mark  <创建 && 初始化>

/**
*	@brief	创建人族
*
*	@return	人族
*/
static Human* create();

/**
*	@brief	初始化人族
*
*	@return	true: 初始化成功 false: 初始化失败
*/
bool init();

};

#endif /* defined(__DesignPattern_Factory__Race__) */


//
//  Human.cpp
//  DesignPattern_Factory
//
//  Created by cc on 14-6-28.
//
//

#include "Human.h"

#pragma mark  <构造 && 析构>

/**
*	@brief	构造
*
*/
Human::~Human() {

}

/**
*	@brief	析构
*
*/
Human::Human(){

}

#pragma mark  <创建 && 初始化>

/**
*	@brief	创建人族
*
*	@return	人族
*/
Human* Human::create() {

Human *pRet = new Human();
if (pRet && pRet->init()) {
pRet->autorelease();
return pRet;
}

CC_SAFE_DELETE(pRet);
return NULL;

}

/**
*	@brief	初始化人族
*
*	@return	true: 初始化成功 false: 初始化失败
*/
bool Human::init(){

this->m_name = "人族";
this->m_features = "hum.png";

if (initWithFile(this->m_features.c_str())) {

CCLabelTTF* pLabName = CCLabelTTF::create(this->m_name.c_str(), "Marker Felt", 22.0f);
pLabName->setPosition(ccp(this->getContentSize().width / 2, this->getContentSize().height + 30.0f));
this->addChild(pLabName, 1);

return true;
}

return false;
}
好了~~~ 兽族和人类两个类写完了,我们想要在兽族和人族这两个子类中分别显示出自己种族的名字,和图片,既然这两个类都属于种族类,那么我们就可以由Race类来统一的管理和创建这两个类的实例,假如我们想创建人族Human类的对象时,只需告诉Race类,我要创建人族类的对象就行了,这时候Race类就是一个对象的生产工厂,只要是他的子类,都有他来统一创建,是不是很方便,看一下下面一段代码。

//
//  IRaceConst.h
//  DesignPattern_Factory
//
//  Created by ChengChao on 14-6-28.
//
//

#ifndef __DesignPattern_Factory__IRaceConst__
#define __DesignPattern_Factory__IRaceConst__

/**
*	@brief	保存种族常量的接口
*/
class IRaceConst {

public:

//种族类型
enum RaceType {
eRaceTypeNone,
eRaceTypeHuman,     //人族
eRaceTypeOrc,       //兽人
eRaceTypeUd,		//亡灵
eRaceTypeNe			//精灵
};

};

#endif /* defined(__DesignPattern_Factory__IRaceConst__) */


/**
*	@brief	创建种族
*
*	@return	种族
*/
Race* Race::createRaceByType(int aRaceType) {

Race* pRace = NULL;

switch (aRaceType) {
case IRaceConst::eRaceTypeHuman:
//人族
pRace = Human::create();
break;
case IRaceConst::eRaceTypeOrc:
//兽族
pRace = Orc::create();
break;
case IRaceConst::eRaceTypeUd:
//亡灵族
pRace = Ud::create();
break;
default:
break;
}

return pRace;
}
这个方法是Race类的一个静态static 工厂方法,通过传入种族的枚举类型,统一由Race类来创建子类的对象,我们想要一个人族,就传一个人族类型,想要一个兽族,就传一个兽族类型,我们把人族和兽族添加到场景里去,我们来看看运行效果。
HelloWorldScene中:

Race* pHumanRace = Race::createRaceByType(IRaceConst::eRaceTypeHuman);
pHumanRace->setPosition(ccp(100, 100));
this->addChild(pHumanRace, 1);

Race* pOrcRace = Race::createRaceByType(IRaceConst::eRaceTypeOrc);
pOrcRace->setPosition(ccp(400, 100));
this->addChild(pOrcRace, 1);
运行效果如下:



如果我们现在又有了一个新的需求,加了一个亡灵族呢? 只需要再创建一个亡灵族类继承种族类Race,然后给亡灵类指定一个类型加到保存种族常量的接口IRaceConst里去,再通过静态工厂方法Race::createRaceByType()创建亡灵类的实例来,把亡灵加到场景里就行了,把代码贴出来,大家可以自己试试~

最后附上源码: http://download.csdn.net/detail/oktears/7568355

本文由CC原创总结,如需转载请注明出处:/article/8540030.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: