Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.

Similar presentations


Presentation on theme: "Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology."— Presentation transcript:

1 Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design pattern Course Fall 2010

2 Restaurant Children’s meal: a main item a side item a drink a toy 2

3 3

4 Intent Separate the construction of a complex object from its representation so that the same construction process can create different representations. 4

5 Differences… Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately. Builder often builds a Composite. 5

6 Motivation Reader for RTF (Rich Text Format) should be able to convert to any other representation ASCII text, text, text widget Open ended number of representations possible Abstract the conversion process 6

7 7

8 Applicability When algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled The construction process must allow different representations for the object that is constructed 8

9 Structure 9

10 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. 10

11 11

12 Collaborations The client creates a Director object and configures it with the desired Builder object Director notifies Builder whenever a part of the product should be built Builder handles requests from the Director and adds parts to the product Client retrieves the product from the Builder 12

13 Sample code 13

14 14

15 15

16 16

17 17

18 18

19 Consequences Benefits: A Builder lets you vary the internal representation of the product it builds. It also hides the details of how the product is assembled. Each specific builder is independent of the others and of the rest of the program. This improves modularity and makes the addition of other builders relatively simple. 19

20 Consequences Benefits: Because each builder constructs the final product step-by-step, depending on the data, you have more control over each final product that a Builder constructs. Liabilities: sometimes you might need access to parts of the product constructed earlier 20

21 Implementation issues the Builder class interface must be general enough to allow the construction of products for all kinds of concrete builders Products don’t have any abstract class client is in a position to know which concrete subclass of Builder is in use and can handle its products accordingly. Empty methods as default in Builder. In C++, the build methods are intentionally not declared pure virtual member functions. They're defined as empty methods instead, letting clients override only the operations they're interested in. 21

22 Related Patterns Abstract Factory focuses on families of product objects, while Builder focuses on step by step construction of complex objects Builder frequently builds a Composite Builder can use Singleton in their implementations 22


Download ppt "Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology."

Similar presentations


Ads by Google