创建型设计模式之Builder

一、使用意图

Separate the construction of a complex object from its representation so that the same construction process can create different representations.
使用例子:
富文本reader和converter,见p111
The Builder pattern separates the algorithm for interpreting a textual format (that is, the parser for RTF documents) from how a converted format gets created and represented. This lets us reuse the RTFReader’s parsing algorithm to create different text representations from RTF documents—just configure the RTFReader with different subclasses of TextConverter.例如,可以创建纯ASCIIText,TeXText,TextWidget这三种不同表现形式的文本。

二、UML图

创建型设计模式之Builder
GoF中,Director与Builder的关系是聚合,但看Sample Code里,Construct(Builder& builder),我觉得是依赖关系。

以参考资料[2]为例,假设Product是Computer,Computer由CPU,RAM,GPU组成,即Computer持有这些类的成员(上图的Part),Computer有setCPU()函数。
ConcreteBuilder有一个私有成员m_pComputer,ConreteBuilder的所有BuildPart()函数为BuildCPU(),BuildRAM(),BuildGPU(),这些函数内部的代码类似:{CPU* cpu = new CPU(); m_pComputer->setCPU(cpu);} ConcreteBuilder的GetResult()返回m_pComputer。
Director的Construct()函数依次执行builder->BuildCPU(), builder->BuildRAM()以及builder->BuildGPU()。

三、组织结构(building blocks)

(1)Builder (TextConverter)
specifies an abstract interface for creating parts of a Product object.
(2)ConcreteBuilder (ASCIIConverter, TeXConverter, TextWidgetConverter)

  • constructs and assembles parts of the product by implementing the Builder interface.
  • defines and keeps track of the representation it creates.
  • provides an interface for retrieving the product (e.g., GetASCIIText, GetTextWidget).

(3)Director (RTFReader)
constructs an object using the Builder interface.
(4)Product (ASCIIText, TeXText, TextWidget)

  • represents the complex object under construction. ConcreteBuilder builds the product’s internal representation and defines the process by which it’s assembled.
  • includes classes that define the constituent parts, including interfaces for assembling the parts into the final result.
    这条见 二、UML图的Computer例子

四、Client使用时的时序图

创建型设计模式之Builder

五、特性

(1)It lets you vary a product’s internal representation.
The Builder object provides the director with an abstract interface for constructing the product. The interface lets the builder hide the representation and internal structure of the product. It also hides how the product gets assembled.
(2)It isolates code for construction and representation.
Each ConcreteBuilder contains all the code to create and assemble a particular kind of product.
Clients needn’t know anything about the classes that define the product’s internal structure; such classes don’t appear in Builder’s interface.
(3)It gives you finer control over the construction process.
Unlike creational patterns that construct products in one shot, the Builder pattern constructs the product step by step under the director’s control.

六、实现注意点

(1)Assembly and construction interface.
Builders construct their products in step-by-step fashion.
(2)Why no abstract class for products?
In the common case, the products produced by the concrete builders differ so greatly in their representation.
(3)Empty methods as default in Builder.
对C++,不声明成纯虚函数,而是函数体为空。
因为子类没必要实现全部函数,如果这些函数在基类为纯虚函数,则子类有可能仍为抽象类。

七、相关设计模式

Abstract Factory (99) is similar to Builder in that it too may construct complex objects. The primary difference is that the Builder pattern focuses on constructing a complex object step by step. Abstract Factory’s emphasis is on families of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory pattern is concerned, the product gets returned
immediately.
A Composite (183) is what the builder often builds.

八、参考资料

[1] GoF设计模式
[2] C++ 建造者模式 作者:一去丶二三里