再次研究设计模式 第二章 生成器

 

1 Builder生成器)

目的

  • 把复杂对象的创建工作从表示中分离出来,同样的创建过程能够有不同的创建行为 Separate the construction of a complex object from its representation so that the same construction process can create different representations.

结构

 

  • Builder : 创建一个产品对象的抽象接口。 specifies an abstract interface for creating parts of a Product object.

  • ConcreteBuilder : 实现了Builder接口的产品的创建和组装者,定义并且跟踪它所创建的内容,提供一个接口来获取他创建的产品。(如 GetASCIIText, GetTextWidget 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 : 创建一个对象来使用Builder接口 Constructs an object using the Builder interface.

  • Product : 创建的复杂的对象,ConcreteBuilder 创建了产品的内容并通过产品组装方式确定了创建程。 Represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled.

通信方式

 

  • 客户代码创建Director对象并且通过指定的 Builder对象来配置它。 The client creates the Director object and configures it with the desired Builder object.

  • 当有产品的某一部分需要被创建时,Director通知Builder创建。 Director notifies the builder whenever a part of the product should be built.

  • Builder处理Director的要求,添加产品的一部分。 Builder handles requests from the director and adds parts to the product.

  • 客户代码从builder中获取创建的产品。 The client retrieves the product from the builder.

  • 与抽象工厂(Abstract Factory)的差别,多了一层Director,通过Director来创建产品,因此可能创建更为复杂的产品系列来构成一个新的产品。

  • 抽象工厂是创建一系列的产品供客户代码使用,而生成器模式是生成多个产品后组合成一个产品供客户代码使用。

应用场合

  • 创建一个复杂对象的算法独立于对象的各个组成部分以及他们的组装方式。 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.

模式效果

  • 让你的可以改变一个产品的内部表示 It lets you vary a product's internal representation. Builder对象提供了一个Director(装配者)它有一个创建产品的抽象接口。该接口使Builder隐藏了产品的表示和内部结构,同样它隐藏了产品是如何装配的,由于产品通过抽象接口创建的,因此当改变 产品的内部表示时,所有需要做的工作就是定义一个新的Builder he 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. Because the product is constructed through an abstract interface, all you have to do to change the product's internal representation is define a new kind of builder.

  • 它将代码与创建及表示中分离。 It isolates code for construction and representation . 生成器模式通过封装复杂对象创建及表示方式提高了模块化性能。客户代码不需知道任何关于产品内部结构类,这些类是不会在Builder的接口中出现的。 The Builder pattern improves modularity by encapsulating the way a complex object is constructed and represented. Clients needn't know anything about the classes that define the product's internal structure; such classes don't appear in Builder's interface.

  • 使你能更好是控制创建过程。 It gives you finer control over the construction process.  与其它的一次性产品的创建型模式不同,生成器模式是在Director的控制下一步一步地创建产品的,只有当产品完全创建完成后,Director才会从中取得它,因此该模式比其它的创建型模式更多地反应了创建的过程,也就使你能更好地控制创建过程和产品的内部表示。 Unlike creational patterns that construct products in one shot, the Builder pattern constructs the product step by step under the director's control. Only when the product is finished does the director retrieve it from the builder. Hence the Builder interface reflects the process of constructing the product more than other creational patterns. This gives you finer control over the construction process and consequently the internal structure of the resulting product.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值