c# and design patterns builder pattern. 2 c# and design patterns builder pattern object creational...

11
C# and Design Patterns Builder Pattern Builder Pattern

Upload: julianna-heath

Post on 05-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction

C#

and

Desi

gn

Patt

ern

s

Builder PatternBuilder Pattern

Page 2: C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction

2

C#

and

Desi

gn

Patt

ern

s

Builder PatternBuilder Pattern

Object creational patternObject creational pattern Separates construction of complex Separates construction of complex

object from its representationobject from its representation Construct an object step-by-stepConstruct an object step-by-step Usually builds composite objectsUsually builds composite objects

Page 3: C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction

3

C#

and

Desi

gn

Patt

ern

s

Goals and ContextGoals and Context

Create complex/composite objects in Create complex/composite objects in a step-by-step fashiona step-by-step fashion

Client must build complex object Client must build complex object without knowing its internal without knowing its internal constructionconstruction

Separate creator from data providersSeparate creator from data providers Different representations for Different representations for

complex object possiblecomplex object possible

Page 4: C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction

4

C#

and

Desi

gn

Patt

ern

sArchitecture and Classes Architecture and Classes

(1/2)(1/2) Create a Create a BuilderBuilder class class Has methods for constructing the Has methods for constructing the

parts of the productparts of the product Also, a method for retrieving the Also, a method for retrieving the

productproduct The The BuilderBuilder builds the final product builds the final product

piece by piecepiece by piece

Page 5: C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction

5

C#

and

Desi

gn

Patt

ern

sArchitecture and Classes Architecture and Classes

(2/2)(2/2) The The BuilderBuilder must be told how to must be told how to

proceed in the construction processproceed in the construction process To this end, we create a To this end, we create a DirectorDirector

classclass Director uses the Director uses the BuilderBuilder interface interface

Page 6: C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction

6

C#

and

Desi

gn

Patt

ern

s

Class DiagramClass Diagram

+BuildPart()+GetProduct()

ConcreteBuilder

+BuildPart()+GetProduct()

AbstractBuilder

+Build()

Director

Client

Product

Requests build

Uses

Creates

Uses

1

1

1

*

1

1

1

1

Page 7: C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction

7

C#

and

Desi

gn

Patt

ern

s

Sequence DiagramSequence Diagram

:Client

b:ConcreteBuilder

:Director

new

ACK

Build(b)

BuildPartA()

ACK

BuildPartN()

ACK

ACK

GetProduct()

ACK

Page 8: C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction

8

C#

and

Desi

gn

Patt

ern

s

AdvantagesAdvantages

Separation of creator Separation of creator (Director)(Director) and and data providers data providers (Builder)(Builder)

More fine-grained than More fine-grained than FactoryFactory patternspatterns Step-by-step creatingStep-by-step creating

Page 9: C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction

9

C#

and

Desi

gn

Patt

ern

s

Application AreasApplication Areas

Everywhere where you need to Everywhere where you need to create complex objects in a step-by-create complex objects in a step-by-step fashionstep fashion

Page 10: C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction

10

C#

and

Desi

gn

Patt

ern

s

Related PatternsRelated Patterns

CompositeComposite The built objects are typically The built objects are typically

compositescomposites FactoriesFactories

Builder is a more fine-grained version of Builder is a more fine-grained version of factoryfactory

Factory can be used to create builder Factory can be used to create builder objectsobjects

Page 11: C# and Design Patterns Builder Pattern. 2 C# and Design Patterns Builder Pattern Object creational pattern Object creational pattern Separates construction

11

C#

and

Desi

gn

Patt

ern

s

ExerciseExercise