工厂模式是一个创建型的设计模式它允许创造对象时不指定要创建的对象的具体类型。本质上工厂模式是构造函数的一般化。但工厂模式又绕开了构造模式的所有限制,从基本层面来看,工厂模式仅仅是一个普通的方法调用,该调用返回类的实例,它们经常和继承一起使用,即派生类能够重写工厂方法并返回类的实例,常见的做法是使用抽象基类实现工厂模式。
一 普通工厂模式
1. 类图
2. 代码实现
#pragma once #include <iostream> using namespace std; // 抽象渲染类 class IRenderer { public: IRenderer() { cout << "IRenderer Construct" << endl; } virtual ~IRenderer() { cout << "IRenderer Deconstruct" << endl; } virtual void Render() = 0; }; // Opengl渲染类 class COpenglRenderer : public IRenderer { public: COpenglRenderer() { cout << "COpenglRenderer Construct" << endl; } ~COpenglRenderer() { cout << "COpenglRenderer Deconstruct" << endl; } void Render() { cout << "COpenglRenderer Render" << endl; } }; // DirectX渲染类 class CDirectxRenderer : public IRenderer { public: CDirectxRenderer() { cout << "CDirectxRenderer Construct" << endl; } ~CDirectxRenderer() { cout << "CDirectxRenderer Deconstruct" << endl; } void Render() { cout << "CDirectxRenderer Render" << endl; } }; // 渲染器的抽象工厂类 class CRendererFactory { public: CRendererFactory(void) { cout << "CRendererFactory Construct" << endl; } ~CRendererFactory(void) { cout << "CRendererFactory Deconstruct" << endl; } IRenderer* CreateRenderer(const char *RenderType) { if (0 == strcmp(RenderType, "Opengl")) { return new COpenglRenderer(); } else if (0 == strcmp(RenderType,"Directx")) { return new CDirectxRenderer(); } } };
#include "stdio.h" #include "RendererFactory.h"
a
void main() { CRendererFactory *pFactory = new CRendererFactory(); IRenderer *pRenderer = pFactory->CreateRenderer("Opengl"); pRenderer->Render(); delete pRenderer; delete pFactory; return; }
3. 测试
二 扩展工厂模式
上面的普通工厂模式演示了一个完全可行的工厂方法,但它存在一个潜在的缺陷就是若每次要添加一个Renderer类就必须在CRendererFactory类中添加相应判断创建的代码,虽然这个改动不是非常繁重,但这意味着不能为新的派生类添加运行时支持,换句话说,用户不能为系统添加新的渲染器,该问题可以使用下面的扩展工厂模式解决。
1. 扩展思想
(1) 工厂类维护一个映射,此映射将类型名与创建对象的回调关联起来
(2)允许新的派生类通过一对新的方法调用来实现注册和注销
(3)运行时注册新类的能力,允许工厂方法模式为API创建可扩展的插件接口
(4)工厂对象必须保持其状态信息,最好强制要求任意时刻只能创建一个工厂对象,将工厂类实现为单例模式
2. 类图
3. 代码实现
#pragma once #include <iostream> #include <map> using namespace std; // 抽象渲染类 class IRenderer { public: IRenderer() { cout << "IRenderer Construct" << endl; } virtual ~IRenderer() { cout << "IRenderer Deconstruct" << endl; } virtual void Render() = 0; }; // Opengl渲染类 class COpenglRenderer : public IRenderer { public: COpenglRenderer() { cout << "COpenglRenderer Construct" << endl; } ~COpenglRenderer() { cout << "COpenglRenderer Deconstruct" << endl; } void Render() { cout << "COpenglRenderer Render" << endl; } static IRenderer* Create() { return new COpenglRenderer(); } }; // DirectX渲染类 class CDirectxRenderer : public IRenderer { public: CDirectxRenderer() { cout << "CDirectxRenderer Construct" << endl; } ~CDirectxRenderer() { cout << "CDirectxRenderer Deconstruct" << endl; } void Render() { cout << "CDirectxRenderer Render" << endl; } static IRenderer* Create() { return new CDirectxRenderer(); } }; // 渲染器的抽象工厂类 class CRendererFactory { private: CRendererFactory(void); ~CRendererFactory(void); private: typedef IRenderer* (*CreateCallBack)(); public: static CRendererFactory* RendererFactoryInstance(); static void RegisterRenderer(const char *cRenderType, CreateCallBack cb); static void UnregisterRenderer(const char *cRenderType); static IRenderer* CreateRenderer(const char *cRenderType); private: typedef map<const char *, CreateCallBack> CREATECALLBACKMAP; static CREATECALLBACKMAP m_CreateCallBackMap; static CRendererFactory *m_pRendererFactory; };
#include "RendererFactory.h" CRendererFactory* CRendererFactory::m_pRendererFactory = NULL; CRendererFactory::CREATECALLBACKMAP CRendererFactory::m_CreateCallBackMap; CRendererFactory::CRendererFactory( void ) { cout << "CRendererFactory Construct" << endl; } CRendererFactory::~CRendererFactory( void ) { cout << "CRendererFactory Deconstruct" << endl; } CRendererFactory* CRendererFactory::RendererFactoryInstance() { if (m_pRendererFactory == NULL) { m_pRendererFactory = new CRendererFactory(); } return m_pRendererFactory; } void CRendererFactory::RegisterRenderer( const char *cRenderType, CreateCallBack cb ) { m_CreateCallBackMap[cRenderType] = cb; } void CRendererFactory::UnregisterRenderer( const char *cRenderType ) { m_CreateCallBackMap.erase(cRenderType); } IRenderer* CRendererFactory::CreateRenderer( const char *cRenderType ) { CREATECALLBACKMAP::iterator iter = m_CreateCallBackMap.find(cRenderType); if (iter != m_CreateCallBackMap.end()) { return iter->second(); } return NULL; }
#include "stdio.h" #include "RendererFactory.h" void main() { CRendererFactory *pFactory = CRendererFactory::RendererFactoryInstance(); pFactory->RegisterRenderer("Opengl", COpenglRenderer::Create); IRenderer *pRenderer = pFactory->CreateRenderer("Opengl"); pRenderer->Render(); delete pRenderer; pFactory->UnregisterRenderer("Opengl"); return; }
4. 测试