builder.h

来自「设计模式C++源码(other)Builder.rar」· C头文件 代码 · 共 120 行

H
120
字号
/** * builder.h * Implemented by Blueprint Technologies, Inc. */#ifndef _builder_h#define _builder_h/** * Represents the complex object under construction. ConcreteBuilder * builds the product's internal representation and defines the * process by which it's assembled. */class Product{};class Part{};/** * Specifies an abstract interface for creating parts of * a Product object. */class Builder{protected:	Part* partA;	Part* partB;	Part* partC;	Product* product;public:	Builder(): partA(0), partB(0), partC(0), product(0)	{	};	virtual ~Builder()	{		if( partA ) delete partA;		if( partB ) delete partB;		if( partC ) delete partC;		if( product ) delete product;	};	virtual void buildPartA() = 0;	virtual void buildPartB() = 0;	virtual void buildPartC() = 0;};/** * Constructs and assembles parts of the product by implementing * the Builder interface. Defines and keeps track of the * representation is creates. Provides an interface for retrieving * the product. */class ConcreteBuilder: public Builder{public:	ConcreteBuilder(): Builder()	{	};	virtual void buildPartA()	{		partA = new Part();	};	virtual void buildPartB()	{		partB = new Part();	};	virtual void buildPartC()	{		partC = new Part();	};	Product* getResult()	{		/**		 * assemble the parts into a product.		 */		return new Product();	};};/** * Constructs an object using the Builder interface. */class Director{private:	Builder* builder;public:	Director( Builder* builder )	{		this -> builder = builder;	};	void construct()	{		builder -> buildPartA();		builder -> buildPartB();		builder -> buildPartC();	};};#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?