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

2015年03月18日 11:21 0 点赞 0 评论 更新于 2025-11-21 14:18

简单工厂模式概述

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

在 Cocos2d-x 中使用简单工厂模式的示例

假设我们正在开发一款类似魔兽的 RPG 游戏,游戏中会出现多种种族的角色,例如人族、兽族等。我们将通过以下步骤来实现简单工厂模式。

定义父类

首先,我们需要定义一个共同的父类来表示种族。由于这些种族都有名字和形象,我们在父类中定义相应的属性。以下是 Race.h 的代码:

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

#ifndef __DesignPattern_Factory__Race__
#define __DesignPattern_Factory__Race__

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

class Race : public cocos2d::CCSprite {
protected:
// 种族名字
std::string m_name;
// 种族形象(用图片表示)
std::string m_features;

public:
// 构造函数
Race();
// 析构函数
virtual ~Race();
};

#endif /* defined(__DesignPattern_Factory__Race__) */

实现子类

接下来,我们实现人族、兽族和亡灵族三个子类。

兽族类

以下是 Orc.hOrc.cpp 的代码:

// 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:
// 构造函数
Orc();
// 析构函数
virtual ~Orc();

// 创建兽族
static Orc* create();
// 初始化兽族
bool init();
};

#endif /* defined(__DesignPattern_Factory__Orc__) */

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

#include "Orc.h"

Orc::~Orc() {
}

Orc::Orc() {
}

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

bool Orc::init() {
this->m_features = "orc.png";
this->m_name = "兽族";
if (initWithFile(this->m_features.c_str())) {
cocos2d::CCLabelTTF* pLabName = cocos2d::CCLabelTTF::create(this->m_name.c_str(), "Marker Felt", 22.0f);
pLabName->setPosition(cocos2d::ccp(this->getContentSize().width / 2, this->getContentSize().height + 30.0f));
this->addChild(pLabName, 1);
return true;
}
return false;
}

人族类

以下是 Human.hHuman.cpp 的代码:

// 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:
// 构造函数
Human();
// 析构函数
virtual ~Human();

// 创建人族
static Human* create();
// 初始化人族
bool init();
};

#endif /* defined(__DesignPattern_Factory__Human__) */

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

#include "Human.h"

Human::~Human() {
}

Human::Human() {
}

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

bool Human::init() {
this->m_name = "人族";
this->m_features = "hum.png";
if (initWithFile(this->m_features.c_str())) {
cocos2d::CCLabelTTF* pLabName = cocos2d::CCLabelTTF::create(this->m_name.c_str(), "Marker Felt", 22.0f);
pLabName->setPosition(cocos2d::ccp(this->getContentSize().width / 2, this->getContentSize().height + 30.0f));
this->addChild(pLabName, 1);
return true;
}
return false;
}

实现工厂方法

为了统一管理和创建这些子类的实例,我们在 Race 类中实现一个静态工厂方法。首先,我们定义一个保存种族常量的接口 IRaceConst.h

// 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__) */

然后,在 Race 类中实现静态工厂方法 createRaceByType

/**
* @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;
}

使用工厂方法创建对象

HelloWorldScene 中,我们可以使用 createRaceByType 方法创建人族和兽族对象,并将它们添加到场景中:

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

Race* pOrcRace = Race::createRaceByType(IRaceConst::eRaceTypeOrc);
pOrcRace->setPosition(cocos2d::ccp(400, 100));
this->addChild(pOrcRace, 1);

扩展功能

如果我们现在需要添加一个新的种族,例如亡灵族,只需要再创建一个亡灵族类继承 Race 类,然后给亡灵类指定一个类型加到 IRaceConst 接口里,再通过静态工厂方法 Race::createRaceByType() 创建亡灵类的实例,最后将亡灵添加到场景中即可。

通过这种方式,我们可以看到简单工厂模式的优势:它将对象的创建和使用分离,使得代码更加清晰、可维护和可扩展。当需要创建新的子类对象时,只需要在工厂方法中添加相应的逻辑,而不需要修改使用对象的代码。

作者信息

feifeila

feifeila

共发布了 3994 篇文章