builder 1

9
Builder Design Pattern

Upload: mahreen-ilahi

Post on 14-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

7/27/2019 Builder 1

http://slidepdf.com/reader/full/builder-1 1/9

Builder Design Pattern

7/27/2019 Builder 1

http://slidepdf.com/reader/full/builder-1 2/9

Builder: Overview

• Intent

 – Separate the construction of a complex object from its

representation so that the same construction process

can create different representations• Think of a car factory

 – Boss tells workers (or 

robots) to build each

part of a car  – Workers build each

part and add them to

the car being constructed

7/27/2019 Builder 1

http://slidepdf.com/reader/full/builder-1 3/9

7/27/2019 Builder 1

http://slidepdf.com/reader/full/builder-1 4/9

Builder: Collaborations

• Client creates Director object and

configures it with a Builder 

• Director notifies Builder to build each part

of the product

• Builder handles requests from Director 

and adds parts to the product

• Client retrieves product from the Builder 

7/27/2019 Builder 1

http://slidepdf.com/reader/full/builder-1 5/9

Builder: Applicability

• Use Builder 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 being

constructed – The building process can be broken down into

discrete steps (difference between Builder 

and Abstract Factory)

7/27/2019 Builder 1

http://slidepdf.com/reader/full/builder-1 6/9

Builder: Consequences

• Lets you vary a product’s internal

representation by using different Builders

• Isolates code for construction and

representation

• Gives finer-grain control over the

construction process

7/27/2019 Builder 1

http://slidepdf.com/reader/full/builder-1 7/9

Builder: Implementation

• Issues to consider:

 – Assembly and construction interface:

generality

 – Is an abstract class for all Products

necessary?

• Usually products don’t have a common interface 

 – Usually there’s an abstract Builder class thatdefines an operation for each component that

a director may ask it to create.

• These operations do nothing by default (empty,

non-virtual methods in C++)

7/27/2019 Builder 1

http://slidepdf.com/reader/full/builder-1 8/9

Difference between Factory &

Builder • The factory pattern defers the choice of what concrete

type of object to make until run time.

• The builder pattern encapsulates the logic of how toput together a complex object so that the client just

requests a configuration and the builder directs thelogic of building it.

• The factory is concerned with what is made, thebuilder with how it is made.

•  Abstract factory's emphasis is on families of productobjects (either simple or complex). Builder returns theproduct as the final step, but as far as the AbstractFactory is concerned, the product gets returnedimmediately.

7/27/2019 Builder 1

http://slidepdf.com/reader/full/builder-1 9/9

Conclusions

• Builder:

 – Lets you vary how a product gets assembled.

 – Can define a new kind of builder for a product

to assemble it in a different way

 – Client doesn’t need to know anything about

the construction process, nor the parts that

make up a product.