Applied Design Patterns with Java
Creational :: Builder (97) {C ch 7}
Collaborations
- The client creates the Director object and configures it with the desired
Builder
object.
- 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.
The following interaction diagram illustrates how Builder and Director cooperate with a client.
Consequences
The Builder pattern has these consequences:
- 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 internal structure of the product. To change the product's internal
representation, simply define a new kind of Builder.
- It isolates code for construction and representation. 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.
Each ConcreteBuilder contains all the code to create and assemble a particular kind of product. The code is written
once; then different Directors can reuse it to build Product variants from the same set of parts. This is exactly
what is meant by the term 'Delegate'.
- 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. 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 allows finer control over the construction and internal structure of the resulting product.
Catalog Creational Prev Next