Builder

来源:互联网 发布:融资租赁数据 编辑:程序博客网 时间:2024/05/22 15:18

Book List:
1. Design_Patterns For Dummies
2. DesignPatterns-Elements of Reusable Object-Oriented Software-1995
3. Software Architecture Design Patterns in Java-2004
4. DesignPatternJavaWorkbook-Steven John Metsker-2002
5. Java Design Patterns A Tutorial (James W. Cooper)
6. design_pattern tutorialsPoint

To create an object including several steps or need to create several sub objects, there are lots of combinations of options to build up the object.

Examples:
 - 1. build robots that build cars and that bakes cookies - Template Method vs Builder Patterns
robots workflow components: {start, test, assemble/bake, stop}
Template Method: decide the workflow steps with methods
Builder Patterns: users want to define workflow themselves. 
CookieRobotBuilder() and AutomotiveRobotBuilder() build the robot and decide workflow
That’s the idea: the client code now sets the sequence and number of the steps in the algorithm, and selects which builders to use to create the robot it wants. For each kind of robot, just design a new builder. The main difference between the Template Method and the Builder design pat- terns is in who creates the sequence of steps in the algorithm. 


 - 2. Intent: Separate the construction of a complex object from its representation so that the same construction process can create different representations.
RTFReader — convert to different tex format: ASC, Tex, Text, each has a builder
Use the Builder pattern when
• 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.


Director(RTFReader, SGMLReader…), -> Builder(TextConverter), -> ConcreteBuilder(ASCIIConverter, TeXConverter, TextWidgetConverter), -> Product(ASCIIText, TeXText, TextWidget)


 - (good) 3. This design may not be effective when the object being created is complex and the series of steps constituting the object creation process can be implemented in different ways producing different representations of the object. Because different implementations of the construction process are all kept within the object, the object can become bulky (construction bloat) and less modular. Subsequently, adding a new implementation or making changes to an existing implementation requires changes to the existing code.
Using the Builder pattern, the process of constructing such an object can be designed more effectively. The Builder pattern suggests moving the construction logic out of the object class to a separate class referred to as a builder class. There can be more than one such builder class each with different implementation for the series of steps to construct the object. Each such builder implementation results in a different representation of the object. 


 - 6. build meal: veg+coke, veg+pepsi, nonveg+coke, nonveg+pepsi


Builder and Abstract Factory

In the case of the Abstract Factory, the client uses the factory's methods to create its own objects. In the Builder's case, the Builder class is instructed on how to create the object and then it is asked for it, but the way that the class is put together is up to the Builder class, this detail making the difference between the two patterns.

0 0
原创粉丝点击