设计模式之五:BUILDER(生成器)—对象创建型模式

2014-03-16 星期日 15:26:39 

Builder,继续GOF。博客园 设计模式之五:BUILDER(生成器)—对象创建型模式

1、Intent

​Separate the construction of a complex object from its representation so that the same construction process can create different representations.

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

2、Also Known As
3、Motivation

​A reader for the RTF (Rich Text Format) document exchange format should be able to convert RTF to many text formats. The reader might convert RTF documents into plain ASCII text or into a text widget that can be edited interactively. The problem, however, is that the number of possible conversions is open-ended. So it should be easy to add a new conversion without modifying the reader.

​一个RTF(Rich Text Format)文档交换格式的阅读器应能将 RTF转换为多种正文格式。该阅读器可以将 RTF文档转换成普通 ASCII文本或转换成一个能以交互方式编辑的正文窗口组件。但问题在于可能转换的数目是无限的。因此要能够很容易实现新的转换的增加,同时却不改变RTF阅读器。


4、Applicability

Use the Builder pattern when

​●  the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled.

​●  the construction process must allow different representations for the object that's constructed.

在以下情况使用B u i l d e r模式

  当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。

  当构造过程必须允许被构造的对象有不同的表示时。

5、Structure


6、代码
#if 1
	class Product1 
	{
	};

	class Product1_Part	//上面的RTF的part也可以独立使用
	{
	};

	class Product2
	{
	};
	
	// class Builder
	class Builder //抽象基类
	{
	public:
		virtual void BuilderPartA() {} //提供缺省实现
		virtual void BuilderPartB() {}
		virtual void BuilderPartC() {}

	protected:
		Builder() {}
	};
	
	// class ConcreteBuilder1
	class ConcreteBuilder1 : public Builder //创建Product1
	{
	public:
		ConcreteBuilder1() : _product(NULL) {}
		virtual void BuilderPartA() {} 
		virtual void BuilderPartB() {}
		virtual void BuilderPartC() {}
		virtual Product1* GetProduct1() { return _product ; } //返回创建的Product1对象

	private:
		Product1 *_product ;
	};
	
	// class ConcreteBuilder2
	class ConcreteBuilder2 : public Builder //创建Product2
	{
	public:
		ConcreteBuilder2() : _product(NULL) {}
		virtual void BuilderPartA() {} 
		virtual void BuilderPartB() {}
		virtual void BuilderPartC() {}
		virtual Product2* GetProduct2() { return _product ; } //返回创建的Product2对象

	private:
		Product2 *_product ;
	};
	
	// class Director
	class Director
	{
	public:
		//创建对象(Director并不知道具体创建出来的对象是什么样的,只有调用该函数的client知道)
		void Construct(Builder *builder)
		{
			builder->BuilderPartA() ;
			builder->BuilderPartB() ;
			builder->BuilderPartC() ;
		}
	};

	//客户端代码:
	void Builder_Demo()
	{
		Director director ;
		// 创建第一种对象
		ConcreteBuilder1 *pBuilder1 = new ConcreteBuilder1();
		director.Construct(pBuilder1);
		Product1 *product1 = pBuilder1->GetProduct1();

		//Product1_Part *Product1_Part = pBuilder1->BuilderPartA();
		//partx 独立使用
		pBuilder1->BuilderPartA();
		
		// 创建第二种对象
		ConcreteBuilder2 *pBuilder2 = new ConcreteBuilder2();
		director.Construct(pBuilder2);
		Product2 *product2 = pBuilder2->GetProduct2();
	}
#endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值