Definition
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
UML Class Diagram
Builder: when and where use it
- The Builder design pattern is a creational pattern that allows the client to construct a complex object by specifying the type and content only. Construction details are hidden from the client entirely. The most common motivation for using Builder is to simplify client code that creates complex objects. The client can still direct the steps taken by the Builder, without knowing how the actual work is accomplished. Builders frequently encapsulate construction of Composite objects (another design pattern) because the procedures involved are often repetitive and complex.
- A scenario where the Builder can be helpful is when building a code generator. Say you‟re writing an application that writes stored procedures for different target databases (SQL Server, Oracle, Db2). The actual output is quite different but the different steps of creating the separate procedures that implement the CRUD statements (Create, Read, Update, Delete) are similar.
- Builder is a creational pattern just like the Factory patterns. However, Builder gives you more control in that each step in the construction process can be customized; Factory patterns create objects in one single step.
Sample
- Product
- BuilderProduct
- SoftwareBuilderProduct
- Provider
- Main
Ref: Gang of Four tutorial
No comments:
Post a Comment