※设计模式※→☆创建型模式☆============Simple Factory模式(二)

来源:互联网 发布:万方数据库医学网 编辑:程序博客网 时间:2024/05/01 14:42

模式概述

        一般情况 下,我们为了提高内聚和松耦合,经常会使用多态来处理一些问题。抽象出一些类的公共接口作为抽象基类或者接口。这样的话,我们将会面临一个挑战。在每次使用子类的时候,我们不得不经常使用base* = New XXX (这也无可厚非,但当系统复杂后,我们可能将无法维护子类的创建),最终我们在程序的扩展和维护成本上的开销将变得越来越大,越来越难。

       我们知道经常“某个对象”由于需求的变化,对象的具体实现面临着剧烈的变化。为了应对这种变化我们抽象出它比较稳定的接口,隔离出“这个易变对象”的变化,从而保持系统中“其它依赖该对象的对象”不随着需求的改变而改变,这就是我们经常谈的Factory模式了。

       我们可以”定义创建对象的接口,并封装对象的创建“。这就是简单Simple Factory模式



模式结构

        Simple Factory模式结构如下:

                            

        

        这里我把SimpleFactory类使用Singleton模式处理了,因为确确实实我只需要一个这样的工厂,而不是需要多个。

                Singleton模式见:

                             http://blog.csdn.net/xiaoting451292510/article/details/8285710

       惯例,我个人习惯是喜欢把拷贝构造函数和赋值函数私有化,并不对其进行实现。以避免一些不必要的麻烦,如果确实是需要使用拷贝构造函数和赋值函数时,再进行一些相关具体情况的处理。



模式讨论

       Simple Factory模式将对象的创建封装了,例如:我们在游戏开发中,经常碰见有N个职业。战士、法师、牧师等等。这里我们可以通过Simple Factory模式来实现。然而其实我们使用Simple Factory模式并不十分完美。上述是使用一个接口通过判别参数类型来确定创建哪个职业,当然也可以通过增加接口的方法来实现创建各个职业的类型。如果我们又新增加了一个职业盗贼。我们就不得不新增加一种类型判断或者新增加一个接口了。这样Factory接口是无法封闭的,也就是我们常说的Close


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

                Singleton模式见:

                                        http://blog.csdn.net/xiaoting451292510/article/details/8290809

       Simple Factory模式也只能解决在一类类对象的创建(即这一类类,有着共同的基类)。如果要为不同类的类提供一个对象创建的接口。我们可以考虑使用AbstractFactory模式。

                Abstract Factory模式见:
                                         http://blog.csdn.net/xiaoting451292510/article/details/8290814



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

               


模式实现



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


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


       C++完整个代码示例(代码在VS2005下测试可运行)

代码及相关下载地址:

              http://download.csdn.net/detail/xiaoting451292510/4905223

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




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* ProfessionA[3] =  {0};


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

#endif


return 0;
}


DESIGNPATTERN_SimpleFactory.h 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
  @file     DESIGNPATTERN_SimpleFactory.h 
  @brief     Define an interface for creating an object, and the package object is created
  @author   arvin
  @version 1.0   2012/12/13
 */


#ifndef CXX_DESIGNPATTERN_SIMPLEFACTORY_H
#define CXX_DESIGNPATTERN_SIMPLEFACTORY_H


class Profession;


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


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

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


/**
* Create Profession
*
* @param VOID
* @return VOID
* @note Profession Type to identify the creating
*/
Profession* CreateProfession(int iProfessionType);
protected:
private:
/**
* Construction
*
* @param VOID
* @return 
*/
SimpleFactory();


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


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


public:
protected:
private:
static SimpleFactory* m_pInstance;
};
#endif /* >>CXX_DESIGNPATTERN_SIMPLEFACTORY_H<< */
/* EOF */


DESIGNPATTERN_SimpleFactory.cpp 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
  @file     DESIGNPATTERN_SimpleFactory.cpp
  @brief     Define an interface for creating an object, and the package object is created
  @author   arvin
  @version 1.0   2012/12/12
 */
#include "stdafx.h"


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


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


SimpleFactory* SimpleFactory::m_pInstance = NULL;




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


}


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


}


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




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


/**
* Create Profession
*
* @param VOID
* @return VOID
* @note Profession Type to identify the creating
*/
Profession* 
SimpleFactory::CreateProfession(int iProfessionType)
{
if (0 == iProfessionType) {
// Warrior
return new Warrior;
}
else if (1 == iProfessionType) {
// Master
return new Master;
}
else {
return NULL;
}
}


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


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


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


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


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


原创粉丝点击