[置顶] ※设计模式※→☆创建型模式☆============Builder模式(五)

模式概述

        Builder模式相当容易让人误解,并走向误区。相信各位也同我一样对Builder模式有着许许多多的疑惑,非常容易与abstract factory模式和Template模式混淆,这些我将在模式实现中仔细同大家一起研究讨论。鉴于Builder模式的误解性比较高,本人也是研究和思考很多人写的一些文章,当然本人此次的文章也仅仅只供大家的参考。开篇前先一起参考下几篇有关还是比较有意义的文章吧!我也无法说哪个对哪个是错误的。在开发过程中,我们根据自己的场合选择适合自己的一种形式即可。

网上相关文章太多,摘录部分如下:

http://blog.csdn.net/depraved_survival/article/details/2309569

http://www.cnblogs.com/happyhippy/archive/2010/09/01/1814287.html

http://redsnow-fenglin.iteye.com/blog/691868

http://www.riabook.cn/doc/designpattern/BuilderPattern.htm

http://www.iteye.com/topic/71175

http://blog.csdn.net/zhongjiekangping/article/details/4557837

http://www.cnblogs.com/bastard/archive/2011/11/21/2257625.html


        统设计中,有时候面临着一个“复杂系统”的创建工作,该对象通常由各个部分的子对象用一定的算法构成,或者说按一定的步骤组合而成;这些的算法和步骤是稳定的,而构成这个对象的子对象却经常由于需求改变而发生变化,此时我们可以选择使用Builder模式尽管Builder模式相当容易让人误解,但我认为这几点应该是不会有错的。因此我个人认为Builder模式中要好好把握如下几点

        1. 需要创建的对象是复杂对象

        2. 对象的创建过程是一步步创建(即:可能由于过程的不同而导致有不同的对象展示)

        3. 明确建造者(Builder)、具体建造者(Concrete Builder)、指导者(Director)、产品(Product)之间的职责和联系。


        建造者(Builder)

                给出一个抽象接口,以规范产品对象的各个组成成分的建造。一般而言,此接口独立于应用程序的商业逻辑。模式中直接创建产品对象的是            具体建造者(ConcreteBuilder):具体建造者类必须实现这个接口所要求的方法:一个是建造方法,另一个是结果返还方法。

        具体建造者(Concrete Builder)

                担任这个角色的是于应用程序紧密相关的类,它们在应用程序调用下创建产品实例。这个角色主要完成的任务包括:

                        实现Builder角色提供的接口,一步一步完成创建产品实例的过程。

                        在建造过程完成后,提供产品的实例。

        指导者(Director)

                担任这个角色的类调用具体建造者角色以创建产品对象。导演者并没有产品类的具体知识,真正拥有产品类的具体知识的是具体建造者对象。

        产品(Product)

                产品便是建造中的复杂对象。

意图:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示



模式结构

Builder模式结构如下:

[置顶] ※设计模式※→☆创建型模式☆============Builder模式(五)_第1张图片

        这里我把所有的子类Builder类使用Singleton模式处理了,因为确确实实我只需要一个这样的具体建造者,而不是需要多个。
                Singleton模式见:
                             http://blog.csdn.net/xiaoting451292510/article/details/8285710


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

模式讨论

        Buider模式可以一步步的进行复杂对象的构建。我们可以处理每一个步骤的细节化工作,使得经过相同的步骤创建最后得到的不同的对象。例如:我们经常使用的电脑,一般由鼠标(Mouse)、键盘(Keyboard)、显示器(Monitor)和主机(Host)构成。当我们组装机器的时候,我们就可以精细的选择各个部件。如果简单一点的,如我代码中的示例。我们可以选择Dell、ASUS还是HP的鼠标等,细化的话,可以继续对相应的鼠标进行加工处理。是要游戏性的鼠标还是办公式的鼠标。

        在项目开发中,经常性各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将他们组合在一起的算法却相对稳定(例如:电脑一般由鼠标(Mouse)、键盘(Keyboard)、显示器(Monitor)和主机(Host)构成)。因此Builder模式的精髓子对象变化较频繁(表示),对算法相对稳定(构建过程)(将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示

       

 后续讨论:

        如下网址,有人深入的说明了一些Builder模式相关和应该注意的地方,当然也应该仅供参考。

        http://www.cnblogs.com/happyhippy/archive/2010/09/01/1814287.html

        作者很注重于闭合性,当然这也是我们每个开发人员理应注重的地方。拿他雨的第二个例子来看好了。他把创建左手、创建右手合成了一个创建手的方法。当然这样就可以创建出各种各样的手了。比如说:三只手,一只手等等。或者也可以一只一只的创建。内部封装我们不必关注。但是毫无疑问,Person类的手也是要从左手+右手 抽象成手了。然后使用手的多态实现手可以操作的功能。因此,我认为原书作者也是没有错的,Builder模式的选择使用方式在于不同的场合你对于现实中情景反应到Coding中的建模情况而定。而不能因Coding的需求来尽量拟合现实的场景。


Builder模式与Abstract Factory模式

       相同点

        1. 二者都是用来创建大的复杂对象 

       不同点

       1. 抽象工厂通过不同的构建过程生成不同的对象表示,而Builder模式通过相同的构建过程生成不同的表示。

       2. 抽象工厂中创建粒度为最终产品;builder模式中创建粒度为产品的各个构件。

       3. 抽象工厂中,Client包含了Director的功能。

       4. 抽象工厂中ProductA、ProductB的具体生成过程可以分别使用buidler模式实现。

       5. 返回对象不同,抽象工厂对象是直接返回的,Builder模式对象不是直接返回的。

       6. 强调的内容不同,抽象工厂强调创建多个相互依赖的对象提供一个同一的接口。Builder模式强制一步步创建对象,并通过相同的创建过程获得不同的结果对象。


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


模式实现

[置顶] ※设计模式※→☆创建型模式☆============Builder模式(五)_第2张图片

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

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

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


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

#ifndef CXX_MOUSE_H
#include "Mouse.h"
#endif

#ifndef CXX_KEYBOARD_H
#include "Keyboard.h"
#endif

#ifndef CXX_MONITOR_H
#include "Monitor.h"
#endif

#ifndef CXX_HOST_H
#include "Host.h"
#endif

#ifndef CXX_DESIGNPATTERN_ABSTRACTFACTORY_H
#include "DESIGNPATTERN_AbstractFactory.h"
#endif

#ifndef CXX_DESIGNPATTERN_DIRECTOR_H
#include "CXX_DESIGNPATTERN_Director.h"
#endif

#ifndef CXX_DESIGNPATTERN_BUILDER_H
#include "DESIGNPATTERN_Builder.h"
#endif

#ifndef CXX_COMPUTER_H
#include "Computer.h"
#endif


int _tmain(int argc, _TCHAR* argv[])
{
#if DEBG_TEST_SINGLETON
	cout<<"***************************Singleton***************************"<<endl;
	Singleton* pSingletonA = NULL;
	Singleton* pSingletonB =  NULL;
	
	pSingletonA = Singleton::Instance();
	pSingletonB = Singleton::Instance();

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

#if DEBG_TEST_SIMPLEFACTORY
	cout<<"***************************SimpleFactory***************************"<<endl;
	Singleton* pSingletonA = NULL;
	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
	cout<<"***************************ComplexFactory***************************"<<endl;
	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


#if DEBG_TEST_ABSTRACTFACTORY
	cout<<"***************************AbstractFactory***************************"<<endl;
	AbstractFactory* pDELLFactory = DELLFactory::Instance();
	AbstractFactory* pASUSFactory = ASUSFactory::Instance();
	AbstractFactory* pHPFactory = HPFactory::Instance();

	//DELL product
	Mouse* pDellMouse = pDELLFactory->CreateMouse();
	Keyboard* pDellKeyboard = pDELLFactory->CreateKeyboard();
	Monitor* pDellMonitor = pDELLFactory->CreateMonitor();
	Host* pDellHost = pDELLFactory->CreateHost();

	//ASUS product
	Mouse* pASUSMouse = pASUSFactory->CreateMouse();
	Keyboard* pASUSKeyboard = pASUSFactory->CreateKeyboard();
	Monitor* pASUSMonitor = pASUSFactory->CreateMonitor();
	Host* pASUSHost = pASUSFactory->CreateHost();

	//HP product
	Mouse* pHPMouse = pHPFactory->CreateMouse();
	Keyboard* pHPKeyboard = pHPFactory->CreateKeyboard();
	Monitor* pHPMonitor = pHPFactory->CreateMonitor();
	Host* pHPHost = pHPFactory->CreateHost();



	if (NULL != pDellMouse) {
		delete pDellMouse;
		pDellMouse = NULL;
	}
	if (NULL != pDellKeyboard) {
		delete pDellKeyboard;
		pDellKeyboard = NULL;
	}
	if (NULL != pDellMonitor) {
		delete pDellMonitor;
		pDellMonitor = NULL;
	}
	if (NULL != pDellHost) {
		delete pDellHost;
		pDellHost = NULL;
	}

	if (NULL != pHPMouse) {
		delete pHPMouse;
		pHPMouse = NULL;
	}
	if (NULL != pHPKeyboard) {
		delete pHPKeyboard;
		pHPKeyboard = NULL;
	}
	if (NULL != pHPMonitor) {
		delete pHPMonitor;
		pHPMonitor = NULL;
	}
	if (NULL != pHPHost) {
		delete pHPHost;
		pHPHost = NULL;
	}
	
	if (NULL != pASUSMouse) {
		delete pASUSMouse;
		pASUSMouse = NULL;
	}
	if (NULL != pASUSKeyboard) {
		delete pASUSKeyboard;
		pASUSKeyboard = NULL;
	}
	if (NULL != pASUSMonitor) {
		delete pASUSMonitor;
		pASUSMonitor = NULL;
	}
	if (NULL != pASUSHost) {
		delete pASUSHost;
		pASUSHost = NULL;
	}
#endif

#if DEBG_TEST_BUILDER
	cout<<"***************************Builder***************************"<<endl;
	
	Director* pDellDirector = new Director(DELLBuilder::Instance());
	AbstractComputer* pDellComputer =  pDellDirector->CreateComputer();
	
	Director* pASUSDirector = new Director(ASUSBuilder::Instance());
	AbstractComputer* pASUSComputer =  pASUSDirector->CreateComputer();

	Director* pHPDirector = new Director(HPBuilder::Instance());
	AbstractComputer* pHPComputer =  pHPDirector->CreateComputer();


	if (NULL != pDellComputer) {
		delete pDellComputer;
		pDellComputer = NULL;
	}
	if (NULL != pASUSComputer) {
		delete pASUSComputer;
		pASUSComputer = NULL;
	}
	if (NULL != pHPComputer) {
		delete pHPComputer;
		pHPComputer = NULL;
	}
#endif

	return 0;
}






















DESIGNPATTERN_Builder.h 


/**
  @file     	DESIGNPATTERN_Builder.h 
  @brief    	1.When the algorithm to create complex object should be independent of the part of the object, 
                  as well as their assembly;
				2.When construction process must be allowed to construct the object is different;
  @author  	arvin
  @version 	1.0   2012/12/20
 */

#ifndef CXX_DESIGNPATTERN_BUILDER_H
#define CXX_DESIGNPATTERN_BUILDER_H

class AbstractComputer;
class Mouse;
class Keyboard;
class Monitor;
class Host;

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

	/**
	* Create Computer
	*
	* @param VOID
	* @return AbstractComputer*
	* @note
	*/
	virtual AbstractComputer* CreateComputer();

protected:
	/**
	* Construction
	*
	* @param VOID
	* @return 
	*/
	Builder();

private:
	/**
	* Copy Construction
	*
	* @param const Builder& cBuilder
	* @return 
	*/
	Builder(const Builder& cBuilder);

	/**
	* Assignment
	*
	* @param const Builder& cBuilder
	* @return Builder&
	*/
	Builder& operator=(const Builder& cBuilder);

	/**
	* Build Mouse
	*
	* @param VOID
	* @return Mouse*
	* @note build and process product
	*/
	virtual Mouse* BuildMouse() = 0;

	/**
	* Build Keyboard
	*
	* @param VOID
	* @return Keyboard*
	* @note build and process product
	*/
	virtual Keyboard* BuildKeyboard() = 0;

	/**
	* Build Monitor
	*
	* @param VOID
	* @return Monitor*
	* @note build and process product
	*/
	virtual Monitor* BuildMonitor() = 0;

	/**
	* Build Host
	*
	* @param VOID
	* @return Host*
	* @note build and process product
	*/
	virtual Host* BuildHost() = 0;

public:
protected:
private:
};

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

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

protected:
private:
	/**
	* Construction
	*
	* @param VOID
	* @return 
	*/
	DELLBuilder();

	/**
	* Copy Construction
	*
	* @param const DELLBuilder& cDELLBuilder
	* @return 
	*/
	DELLBuilder(const DELLBuilder& cDELLBuilder);

	/**
	* Assignment
	*
	* @param const DELLBuilder& cDELLBuilder
	* @return DELLBuilder&
	*/
	DELLBuilder& operator=(const DELLBuilder& cDELLBuilder);

	/**
	* Build Mouse
	*
	* @param VOID
	* @return Mouse*
	* @note build and process product
	*/
	virtual Mouse* BuildMouse();

	/**
	* Build Keyboard
	*
	* @param VOID
	* @return Keyboard*
	* @note build and process product
	*/
	virtual Keyboard* BuildKeyboard();

	/**
	* Build Monitor
	*
	* @param VOID
	* @return Monitor*
	* @note build and process product
	*/
	virtual Monitor* BuildMonitor();

	/**
	* Build Host
	*
	* @param VOID
	* @return Host*
	* @note build and process product
	*/
	virtual Host* BuildHost();

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


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

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

protected:
private:
	/**
	* Construction
	*
	* @param VOID
	* @return 
	*/
	ASUSBuilder();

	/**
	* Copy Construction
	*
	* @param const ASUSBuilder& cASUSBuilder
	* @return 
	*/
	ASUSBuilder(const ASUSBuilder& cASUSBuilder);

	/**
	* Assignment
	*
	* @param const ASUSBuilder& cASUSBuilder
	* @return ASUSBuilder&
	*/
	ASUSBuilder& operator=(const ASUSBuilder& cASUSBuilder);

	/**
	* Build Mouse
	*
	* @param VOID
	* @return Mouse*
	* @note build and process product
	*/
	virtual Mouse* BuildMouse();

	/**
	* Build Keyboard
	*
	* @param VOID
	* @return Keyboard*
	* @note build and process product
	*/
	virtual Keyboard* BuildKeyboard();

	/**
	* Build Monitor
	*
	* @param VOID
	* @return Monitor*
	* @note build and process product
	*/
	virtual Monitor* BuildMonitor();

	/**
	* Build Host
	*
	* @param VOID
	* @return Host*
	* @note build and process product
	*/
	virtual Host* BuildHost();

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

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

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

protected:
private:
	/**
	* Construction
	*
	* @param VOID
	* @return 
	*/
	HPBuilder();

	/**
	* Copy Construction
	*
	* @param const HPBuilder& cHPBuilder
	* @return 
	*/
	HPBuilder(const HPBuilder& cHPBuilder);

	/**
	* Assignment
	*
	* @param const HPBuilder& cHPBuilder
	* @return HPBuilder&
	*/
	HPBuilder& operator=(const HPBuilder& cHPBuilder);

	/**
	* Build Mouse
	*
	* @param VOID
	* @return Mouse*
	* @note build and process product
	*/
	virtual Mouse* BuildMouse();

	/**
	* Build Keyboard
	*
	* @param VOID
	* @return Keyboard*
	* @note build and process product
	*/
	virtual Keyboard* BuildKeyboard();

	/**
	* Build Monitor
	*
	* @param VOID
	* @return Monitor*
	* @note build and process product
	*/
	virtual Monitor* BuildMonitor();

	/**
	* Build Host
	*
	* @param VOID
	* @return Host*
	* @note build and process product
	*/
	virtual Host* BuildHost();

public:
protected:
private:
	static HPBuilder* m_pInstance;
};
#endif	/* >>CXX_DESIGNPATTERN_BUILDER_H<< */
/* EOF */

DESIGNPATTERN_Builder.cpp 



















/**
  @file     	DESIGNPATTERN_Builder.h 
  @brief    	1.When the algorithm to create complex object should be independent of the part of the object, 
                  as well as their assembly;
				2.When construction process must be allowed to construct the object is different;
  @author  	arvin
  @version 	1.0   2012/12/20
 */

#include "stdafx.h"

#ifndef CXX_DESIGNPATTERN_BUILDER_H
#include "DESIGNPATTERN_Builder.h"
#endif

#ifndef CXX_DESIGNPATTERN_ABSTRACTFACTORY_H
#include "DESIGNPATTERN_AbstractFactory.h"
#endif

#ifndef CXX_COMPUTER_H
#include "Computer.h"
#endif

#ifndef CXX_MOUSE_H
#include "Mouse.h"
#endif

#ifndef CXX_KEYBOARD_H
#include "Keyboard.h"
#endif

#ifndef CXX_MONITOR_H
#include "Monitor.h"
#endif

#ifndef CXX_HOST_H
#include "Host.h"
#endif


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

}

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

}


/**
* Create Computer
*
* @param VOID
* @return AbstractComputer*
* @note
*/
AbstractComputer* 
Builder::CreateComputer()
{
	//build Mouse;
	Mouse* pMouse = BuildMouse();
	//build Keyboard;
	Keyboard* pKeyboard = BuildKeyboard();
	//build Monitor;
	Monitor* pMonitor = BuildMonitor();
	//build Host;
	Host* pHost = BuildHost();

	return new Computer(pMouse, pKeyboard, pMonitor, pHost);
}


/**
* Build Mouse
*
* @param VOID
* @return Mouse*
* @note build and process product
*/
Mouse* 
Builder::BuildMouse()
{
	//system error log
	cerr<<"[Builder::BuildMouse] got called from thread "<</*ThreadID*/"at: "/**gettimeofday()*/<<endl;

	return NULL;
}

/**
* Build Keyboard
*
* @param VOID
* @return Keyboard*
* @note build and process product
*/
Keyboard* 
Builder::BuildKeyboard()
{
	//system error log
	cerr<<"[Builder::BuildKeyboard] got called from thread "<</*ThreadID*/"at: "/**gettimeofday()*/<<endl;

	return NULL;
}
/**
* Build Monitor
*
* @param VOID
* @return Monitor*
* @note build and process product
*/
Monitor* 
Builder::BuildMonitor()
{
	//system error log
	cerr<<"[Builder::BuildMonitor] got called from thread "<</*ThreadID*/"at: "/**gettimeofday()*/<<endl;

	return NULL;
}

/**
* Build Host
*
* @param VOID
* @return Host*
* @note build and process product
*/
Host* 
Builder::BuildHost()
{
	//system error log
	cerr<<"[Builder::BuildHost] got called from thread "<</*ThreadID*/"at: "/**gettimeofday()*/<<endl;

	return NULL;
}



DELLBuilder* DELLBuilder::m_pInstance = NULL;

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

}

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

}

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


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

/**
* Build Mouse
*
* @param VOID
* @return Mouse*
* @note build and process product
*/
Mouse* 
DELLBuilder::BuildMouse()
{
	return DELLFactory::Instance()->CreateMouse();
}

/**
* Build Keyboard
*
* @param VOID
* @return Keyboard*
* @note build and process product
*/
Keyboard* 
DELLBuilder::BuildKeyboard()
{
	return DELLFactory::Instance()->CreateKeyboard();
}

/**
* Build Monitor
*
* @param VOID
* @return Monitor*
* @note build and process product
*/
Monitor* 
DELLBuilder::BuildMonitor()
{
	return DELLFactory::Instance()->CreateMonitor();
}

/**
* Build Host
*
* @param VOID
* @return Host*
* @note build and process product
*/
Host* 
DELLBuilder::BuildHost()
{
	return DELLFactory::Instance()->CreateHost();
}


ASUSBuilder* ASUSBuilder::m_pInstance = NULL;

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

}

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

}

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


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

/**
* Build Mouse
*
* @param VOID
* @return Mouse*
* @note build and process product
*/
Mouse* 
ASUSBuilder::BuildMouse()
{
	return ASUSFactory::Instance()->CreateMouse();
}

/**
* Build Keyboard
*
* @param VOID
* @return Keyboard*
* @note build and process product
*/
Keyboard* 
ASUSBuilder::BuildKeyboard()
{
	return ASUSFactory::Instance()->CreateKeyboard();
}

/**
* Build Monitor
*
* @param VOID
* @return Monitor*
* @note build and process product
*/
Monitor* 
ASUSBuilder::BuildMonitor()
{
	return ASUSFactory::Instance()->CreateMonitor();
}

/**
* Build Host
*
* @param VOID
* @return Host*
* @note build and process product
*/
Host* 
ASUSBuilder::BuildHost()
{
	return ASUSFactory::Instance()->CreateHost();
}



HPBuilder* HPBuilder::m_pInstance = NULL;

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

}

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

}

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


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

/**
* Build Mouse
*
* @param VOID
* @return Mouse*
* @note build and process product
*/
Mouse* 
HPBuilder::BuildMouse()
{
	return HPFactory::Instance()->CreateMouse();
}

/**
* Build Keyboard
*
* @param VOID
* @return Keyboard*
* @note build and process product
*/
Keyboard* 
HPBuilder::BuildKeyboard()
{
	return HPFactory::Instance()->CreateKeyboard();
}

/**
* Build Monitor
*
* @param VOID
* @return Monitor*
* @note build and process product
*/
Monitor* 
HPBuilder::BuildMonitor()
{
	return HPFactory::Instance()->CreateMonitor();
}

/**
* Build Host
*
* @param VOID
* @return Host*
* @note build and process product
*/
Host* 
HPBuilder::BuildHost()
{
	return HPFactory::Instance()->CreateHost();
}
/* EOF */

CXX_DESIGNPATTERN_Director.h 
/**
  @file     	DESIGNPATTERN_Director.h 
  @brief    	1.When the algorithm to create complex object should be independent of the part of the object, 
                  as well as their assembly;
				2.When construction process must be allowed to construct the object is different;
  @author  	arvin
  @version 	1.0   2012/12/20
 */

#ifndef CXX_DESIGNPATTERN_DIRECTOR_H
#define CXX_DESIGNPATTERN_DIRECTOR_H

class AbstractComputer;
class Builder;

class Director
{
public:
	/**
	* Construction
	*
	* @param Builder* pBuilder
	* @return 
	*/
	Director(Builder* pBuilder);

	/**
	* Destruction
	*
	* @param VOID
	* @return 
	*/
	~Director();

	/**
	* Create Computer
	*
	* @param VOID
	* @return AbstractComputer*
	* @note
	*/
	AbstractComputer* CreateComputer();

protected:

private:
	/**
	* Copy Construction
	*
	* @param const Director& cDirector
	* @return 
	*/
	Director(const Director& cDirector);

public:
protected:
private:
	Builder* m_pBuilder;
};

#endif	/* >>CXX_DESIGNPATTERN_DIRECTOR_H<< */
/* EOF */

CXX_DESIGNPATTERN_Director.cpp 
/**
  @file     	DESIGNPATTERN_Director.h 
  @brief    	1.When the algorithm to create complex object should be independent of the part of the object, 
                  as well as their assembly;
				2.When construction process must be allowed to construct the object is different;
  @author  	arvin
  @version 	1.0   2012/12/20
 */
#include "stdafx.h"

#ifndef CXX_DESIGNPATTERN_DIRECTOR_H
#include "CXX_DESIGNPATTERN_Director.h"
#endif

#ifndef CXX_DESIGNPATTERN_BUILDER_H
#include "DESIGNPATTERN_Builder.h"
#endif

#ifndef CXX_DESIGNPATTERN_ABSTRACTFACTORY_H
#include "DESIGNPATTERN_AbstractFactory.h"
#endif

#ifndef CXX_COMPUTER_H
#include "Computer.h"
#endif

/**
* Construction
*
* @param Builder* pBuilder
* @return 
*/
Director::Director(Builder* pBuilder):
m_pBuilder(pBuilder)
{

}

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

/**
* Create Computer
*
* @param VOID
* @return AbstractComputer*
* @note
*/
AbstractComputer* 
Director::CreateComputer()
{
	if (NULL != m_pBuilder) {
		return m_pBuilder->CreateComputer();
	}
	return NULL;
}
/* EOF */



你可能感兴趣的:([置顶] ※设计模式※→☆创建型模式☆============Builder模式(五))