※设计模式※→☆创建型模式☆============Complex Factory模式(三)

模式概述

       一般情况 下,我们为了提高内聚松耦合,经常会使用多态来处理一些问题。抽象出一些类的公共接口作为抽象基类或者接口。这样的话,我们将会面临一个挑战。在每次使用子类的时候,我们不得不经常使用base* = New XXX (这也无可厚非,但当系统复杂后,我们可能将无法维护子类的创建),最终我们在程序的扩展和维护成本上的开销将变得越来越大,越来越难。
       我们知道经常“某个对象”由于需求的变化,对象的具体实现面临着剧烈的变化。为了应对这种变化我们抽象出它比较稳定的接口,隔离出“这个易变对象”的变化,从而保持系统中“其它依赖该对象的对象”不随着需求的改变而改变,这就是我们经常谈的Factory模式了。
       简单Simple Factory模式可以实现 ”定义创建对象的接口,并封装对象的创建“。
             Simple Factory模式见:
 http://blog.csdn.net/xiaoting451292510/article/details/8289608

       然而,Simple Factory模式只解决了一个问题 "对象的创建封装",这个并不是Factory模式的真正意义所在。Factory模式的真正意义或者说他的威力是 ”将具体化类的工作延迟到了类中“,因此实际上我们一般经常使用的是ComplexFactory模式。

 

Complex Factory模式由以下角色组成

  • 抽象工厂(Factory)角色:工厂方法模式的核心,任何在模式中创建对象的工厂类必须实现这个接口。实际工作中,这个角色也常常使用抽象类实现。(或者使用接口+抽象类更好的封装)
  • 具体工厂(ConcreteFactory)角色:实现了抽象工厂接口的具体类,含有与业务密切相关的逻辑,并且受到使用者的调用以创建产品类。
  • 抽象(Product)产品:工厂方法模式所创建的对象的抽象类,也就是所有产品类的共同父类或共同拥有的接口。实际工作中,这个角色也常常使用抽象类实现。(或者使用接口+抽象类更好的封装,如上就运用抽象类+接口+继承)
  • 具体(ConcreteProduct)产品:实现了实现了抽象运算所声明的接口,工厂方法模式所创建的每一个对象都是某个具体产品角色的实例。

模式结构

        Complex Factory模式结构如下:


                            
        
        这里我把ComplexFactory类使用Singleton模式处理了,因为确确实实我只需要一个这样的工厂,而不是需要多个。
                Singleton模式见:
                             http://blog.csdn.net/xiaoting451292510/article/details/8285710
       惯例,我个人习惯是喜欢把拷贝构造函数和赋值函数私有化,并不对其进行实现。以避免一些不必要的麻烦,如果确实是需要使用拷贝构造函数和赋值函数时,再进行一些相关具体情况的处理。

 

 

模式讨论

       Complex Factory模式将对象的创建封装了,例如:我们在游戏开发中,经常碰见有N个职业。战士、法师、牧师等等。这里我们可以通过Complex Factory模式来实现。然而其实我们使用Complex  Factory模式并不十分完美。上述是继承方法来实现创建各个职业的类型。如果我们又新增加了一个职业盗贼。我们就不得不新继承一个子类了。这样我们不得不以新建一个类作为代价


       Complex Factory模式也只能解决在一类类对象的创建(即这一类类,有着共同的基类)。如果要为不同类的类提供一个对象创建的接口。我们可以考虑使用AbstractFactory模式。
                Abstract Factory模式见:
                         http://blog.csdn.net/xiaoting451292510/article/details/8290814


最后,无论模式多么优越,我们也需要根据实际的具体情况而慎重考虑。

 

 

模式实现

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
以后的笔记潇汀会尽量详细讲解一些相关知识的,希望大家继续关注我的博客。
本节笔记到这里就结束了。

潇汀一有时间就会把自己的学习心得,觉得比较好的知识点写出来和大家一起分享。
编程开发的路很长很长,非常希望能和大家一起交流,共同学习,共同进步。
如果文章中有什么疏漏的地方,也请大家指正。也希望大家可以多留言来和我探讨编程相关的问题。
最后,谢谢你们一直的支持~~~


       C++完整个代码示例(代码在VS2005下测试可运行)
代码及相关下载地址:
              http://download.csdn.net/detail/xiaoting451292510/4904578


DesignPattern.cpp 


// DesignPattern.cpp : Defines the entry point for the console application.
//




#include "stdafx.h"




#ifndef CXX_DESIGNPATTERN_SINGLETON_H
#include "DESIGNPATTERN_Singleton.h"
#endif




#ifndef CXX_PROFESSION_H
#include "Profession.h"
#endif




#ifndef CXX_DESIGNPATTERN_SIMPLEFACTORY_H
#include "DESIGNPATTERN_SimpleFactory.h"
#endif




#ifndef CXX_DESIGNPATTERN_COMPLEXFACTORY_H
#include "DESIGNPATTERN_ComplexFactory.h"
#endif








int _tmain(int argc, _TCHAR* argv[])
{
#if DEBG_TEST_SINGLETON
Singleton* pSingletonA = NULL;
Singleton* pSingletonB =  NULL;


pSingletonA = Singleton::Instance();
pSingletonB = Singleton::Instance();




Singleton::Destroy();
Singleton::Destroy();
#endif




#if DEBG_TEST_SIMPLEFACTORY
Profession* pProfession[3] =  {0};




for (int i=0; i<3; i++) {
pProfession[i] = SimpleFactory::Instance()->CreateProfession(i);
}
for (int i=0; i<3; i++) {
if (NULL != pProfession[i]) {
delete pProfession[i];
pProfession[i] = NULL;
}
}


#endif




#if DEBG_TEST_COMPLEXFACTORY
ComplexFactory* pWarriorFactory = WarriorFactory::Instance();
ComplexFactory* pMasterFactory = MasterFactory::Instance();
Profession* pWarrior = pWarriorFactory->CreateProfession();
Profession* pMaster = pMasterFactory->CreateProfession();




if (NULL != pWarrior) {
delete pWarrior;
pWarrior = NULL;
}
if (NULL != pMaster) {
delete pMaster;
pMaster = NULL;
}
#endif












return 0;
}





DESIGNPATTERN_ComplexFactory.h 


/**
  @file     DESIGNPATTERN_ComplexFactory.h
  @brief     1. Define an interface for creating an object, and the package object is created
                2. The work of the specific class of delay to sub-class
  @author   arvin
  @version 1.0   2012/12/14
 */


#ifndef CXX_DESIGNPATTERN_COMPLEXFACTORY_H
#define CXX_DESIGNPATTERN_COMPLEXFACTORY_H


class Profession;


class ComplexFactory
{
public:
/**
* Destruction
*
* @param VOID
* @return 
*/
virtual ~ComplexFactory() = 0;


/**
* Create Profession
*
* @param VOID
* @return Profession* 
* @note
*/
virtual Profession* CreateProfession() = 0;
protected:
/**
* Construction
*
* @param VOID
* @return 
*/
ComplexFactory();


private:


/**
* Copy Construction
*
* @param const ComplexFactory& cComplexFactory
* @return 
*/
ComplexFactory(const ComplexFactory& cComplexFactory);


/**
* Assignment
*
* @param const ComplexFactory& cComplexFactory
* @return ComplexFactory&
*/
ComplexFactory& operator=(const ComplexFactory& cComplexFactory);


public:
protected:
private:
};




class WarriorFactory : public ComplexFactory
{
public:
/**
* Destruction
*
* @param VOID
* @return 
*/
virtual ~WarriorFactory();


/**
* Instance
*
* @param VOID
* @return ComplexFactory*
* @note singleton
*/
static ComplexFactory* Instance();

/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
static VOID Destroy();


/**
* Create Profession
*
* @param VOID
* @return Profession* 
* @note create Warrior
*/
Profession* CreateProfession();
protected:
private:
/**
* Construction
*
* @param VOID
* @return 
*/
WarriorFactory();


/**
* Copy Construction
*
* @param const WarriorFactory& cWarriorFactory
* @return 
*/
WarriorFactory(const WarriorFactory& cWarriorFactory);


/**
* Assignment
*
* @param const WarriorFactory& cWarriorFactory
* @return WarriorFactory&
*/
WarriorFactory& operator=(const WarriorFactory& cWarriorFactory);


public:
protected:
private:
static WarriorFactory* m_pInstance;
};


class MasterFactory : public ComplexFactory
{
public:
/**
* Destruction
*
* @param VOID
* @return 
*/
~MasterFactory();


/**
* Instance
*
* @param VOID
* @return ComplexFactory*
* @note singleton
*/
static ComplexFactory* Instance();

/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
static VOID Destroy();


/**
* Create Profession
*
* @param VOID
* @return Profession* 
* @note create Master
*/
Profession* CreateProfession();
protected:
private:
/**
* Construction
*
* @param VOID
* @return 
*/
MasterFactory();


/**
* Copy Construction
*
* @param const MasterFactory& cMasterFactory
* @return 
*/
MasterFactory(const MasterFactory& cMasterFactory);


/**
* Assignment
*
* @param const MasterFactory& cMasterFactory
* @return MasterFactory&
*/
MasterFactory& operator=(const MasterFactory& cMasterFactory);


public:
protected:
private:
static MasterFactory* m_pInstance;
};
#endif /* >>CXX_DESIGNPATTERN_COMPLEXFACTORY_H<< */
/* EOF */


DESIGNPATTERN_ComplexFactory.cpp 


/**
  @file     DESIGNPATTERN_ComplexFactory.h
  @brief     1. Define an interface for creating an object, and the package object is created
                2. The work of the specific class of delay to sub-class
  @author   arvin
  @version 1.0   2012/12/14
 */
#include "stdafx.h"




#ifndef CXX_DESIGNPATTERN_COMPLEXFACTORY_H
#include "DESIGNPATTERN_ComplexFactory.h"
#endif




#ifndef CXX_PROFESSION_H
#include "Profession.h"
#endif




/**
* Construction
*
* @param VOID
* @return 
*/
ComplexFactory::ComplexFactory()
{




}




/**
* Destruction
*
* @param VOID
* @return 
*/
ComplexFactory::~ComplexFactory()
{




}








/**
* Create Profession
*
* @param VOID
* @return Profession* 
* @note
*/
Profession* 
ComplexFactory::CreateProfession()
{
//system error log
cerr<<"got called from thread "<</*ThreadID*/"at: "/**gettimeofday()*/<<endl;




return NULL;
}












WarriorFactory* WarriorFactory::m_pInstance = NULL;




/**
* Construction
*
* @param VOID
* @return 
*/
WarriorFactory::WarriorFactory()
{




}




/**
* Destruction
*
* @param VOID
* @return 
*/
WarriorFactory::~WarriorFactory()
{




}




/**
* Instance
*
* @param VOID
* @return WarriorFactory*
* @note WarriorFactory
*/
WarriorFactory* 
WarriorFactory::Instance()
{
if (NULL == m_pInstance) {
m_pInstance = new WarriorFactory;
}
return m_pInstance;
}








/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
VOID 
WarriorFactory::Destroy()
{
if (NULL != m_pInstance) {
delete m_pInstance;
m_pInstance = NULL;
}
}




/**
* Create Profession
*
* @param VOID
* @return Profession* 
* @note create Warrior
*/
Profession* 
WarriorFactory::CreateProfession()
{
return new Warrior;
}




MasterFactory* MasterFactory::m_pInstance = NULL;








/**
* Construction
*
* @param VOID
* @return 
*/
MasterFactory::MasterFactory()
{




}




/**
* Destruction
*
* @param VOID
* @return 
*/
MasterFactory::~MasterFactory()
{




}




/**
* Instance
*
* @param VOID
* @return MasterFactory*
* @note MasterFactory
*/
MasterFactory* 
MasterFactory::Instance()
{
if (NULL == m_pInstance) {
m_pInstance = new MasterFactory;
}
return m_pInstance;
}








/**
* Destroy
*
* @param VOID
* @return VOID
* @note singleton
*/
VOID 
MasterFactory::Destroy()
{
if (NULL != m_pInstance) {
delete m_pInstance;
m_pInstance = NULL;
}
}




/**
* Create Profession
*
* @param VOID
* @return Profession* 
* @note create Master
*/
Profession* 
MasterFactory::CreateProfession()
{
return new Master;
}




/* EOF */






Profession.h 


/**
  @file     Profession.h 
  @brief     all kinds of Profession
  @author   arvin
  @version 1.0   2012/12/13
 */




#ifndef CXX_PROFESSION_H
#define CXX_PROFESSION_H




class Profession
{
public:
/**
* Construction
*
* @param VOID
* @return 
*/
Profession();




/**
* Destruction
*
* @param VOID
* @return 
*/
    virtual ~Profession() = 0;




/**
* Operation
*
* @param VOID
* @return 
*/
virtual VOID Operation() = 0;




protected:
private:


/**
* Copy Construction
*
* @param const Profession& cSimpleFactory
* @return 
*/
Profession(const Profession& cSimpleFactory);




/**
* Assignment
*
* @param const Profession& cSimpleFactory
* @return Profession&
*/
Profession& operator=(const Profession& cSimpleFactory);




public:
protected:
private:
};




class Warrior : public Profession
{
public:
/**
* Construction
*
* @param VOID
* @return 
*/
Warrior();




/**
* Destruction
*
* @param VOID
* @return 
*/
virtual ~Warrior();




/**
* Operation
*
* @param VOID
* @return 
*/
virtual VOID Operation();




protected:
private:


/**
* Copy Construction
*
* @param const Warrior& cSimpleFactory
* @return 
*/
Warrior(const Warrior& cSimpleFactory);




/**
* Assignment
*
* @param const Warrior& cSimpleFactory
* @return Warrior&
*/
Warrior& operator=(const Warrior& cSimpleFactory);




public:
protected:
private:
};




class Master : public Profession
{
public:
/**
* Construction
*
* @param VOID
* @return 
*/
Master();




/**
* Destruction
*
* @param VOID
* @return 
*/
virtual ~Master();




/**
* Operation
*
* @param VOID
* @return 
*/
virtual VOID Operation();




protected:
private:


/**
* Copy Construction
*
* @param const Master& cSimpleFactory
* @return 
*/
Master(const Master& cSimpleFactory);




/**
* Assignment
*
* @param const Master& cSimpleFactory
* @return Master&
*/
Master& operator=(const Master& cSimpleFactory);




public:
protected:
private:
};




#endif /* >>CXX_DESIGNPATTERN_SINGLETON_H<< */
/* EOF */





Profession.cpp 
 


/**
  @file     Profession.h 
  @brief     all kinds of Profession
  @author   arvin
  @version 1.0   2012/12/13
 */




#include "stdafx.h"




#ifndef CXX_PROFESSION_H
#include "Profession.h"
#endif




/**
* Construction
*
* @param VOID
* @return 
*/
Profession::Profession()
{
cout<<"Construction Profession"<<endl;
}




/**
* Destruction
*
* @param VOID
* @return 
*/
Profession::~Profession()
{
cout<<"Destruction Profession"<<endl;
}




/**
* Operation
*
* @param VOID
* @return 
*/
VOID 
Profession::Operation()
{
//system error log
cerr<<"got called from thread "<</*ThreadID*/"at: "/**gettimeofday()*/<<endl;
}




/**
* Construction
*
* @param VOID
* @return 
*/
Warrior::Warrior()
{
cout<<"Construction Warrior"<<endl;
}




/**
* Destruction
*
* @param VOID
* @return 
*/
Warrior::~Warrior()
{
cout<<"Destruction Warrior"<<endl;
}




/**
* Operation
*
* @param VOID
* @return 
*/
VOID 
Warrior::Operation()
{




}








/**
* Construction
*
* @param VOID
* @return 
*/
Master::Master()
{
cout<<"Construction Master"<<endl;
}




/**
* Destruction
*
* @param VOID
* @return 
*/
Master::~Master()
{
cout<<"Destruction Master"<<endl;
}




/**
* Operation
*
* @param VOID
* @return 
*/
VOID 
Master::Operation()
{




}




/* EOF */


 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值