原版设计模式之建造者模式
Intent (定义)
Separate the construction of a complex object from its representation,so that the same construction process can create different representations. (将复杂对象的构建从他的展示中分离,以便,相同的构造过程能创建不同的展示。)
Motivation (举例)
以RTF文本文档能转变为各种其他类型的文档为例。
- RTFReader: 指挥者,用于调用建造者创建不同类型的文档格式;
- TextConverter: 抽象建造者
- ASCIITextConverter: ASCII格式的建造者,将RTF文档转变为ASCII格式;
- TexConverter:Tex格式的建造者;
- TextWidgetConverter: Text格式的建造者;
- ASCIIText、TexText、TextWidget:具体建造出来的产品;
代码应该如下所示:
建造者模式
Applicability (哪些情况适用此模式)
- 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.(构造的对象需要有不同的展示)
Client调用Director来进行建造不同的产品。
Participants (上述类图说明)
- Builder (TextConverter) 建造者
- specifies an abstract interface for creating parts of a Product object. (为了创建一个对象或者对象的部分而指定一个抽象接口)
- 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). (提供一个获取产品的接口)
- Director (RTFReader) 指挥者
- constructs an object using the Builder interface. (用建造接口创建一个对象)
- 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. (包含将部件组成最终结果的接口)
Collaborations (约定)
- The client creates the Director object and configures it with the desired Builder object.(Client创建指挥者,并将它需要的建造者注入进去)
- Director notifies the builder whenever a part of the product should be built.(每当要创建一个产品的一部分时,指挥者就会通知建造者)
- Builder handles requests from the director and adds parts to the product.(建造者处理从指挥者那得到的请求,并将部件加载到产品中)
- The client retrieves the product from the builder. (Client从建造者中获取产品)
下图展示了客户端、指挥者、建造者的协作过程,
从上述协作图可以看出,代码应该是如下这样,
public class Client{
ConcreteBuilder aConcreteBuilder = null;
Director aDirector = null;
public Client(){
aConcreteBuilder = new ConcreteBuilder();
aDirector = new Director(aConcreteBuilder );
}
/**
构造产品
*/
public void construct(){
aDirector.BuildPartA();//--最终由Builder构造-->aConcreteBuilder.BuildPartA()
aDirector.BuildPartB();//--最终由Builder构造-->aConcreteBuilder.BuildPartB()
aDirector.BuildPartC();//--最终由Builder构造-->aConcreteBuilder.BuildPartC()
}
/**
从建造器中获取构造好的产品
*/
public Product getResult(){
aConcreteBuilder.getProduct();
}
public static void main(String[] args){
Client client = new Client();
client.construct();
client.getResult();
}
}
Related Patterns (相关模式)
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. (建造者最后一步返回产品,而抽象工厂是立即返回产品。)