c# and design patterns builder pattern. 2 c# and design patterns builder pattern object creational...
TRANSCRIPT
C#
and
Desi
gn
Patt
ern
s
Builder PatternBuilder Pattern
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
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
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
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
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
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
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
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
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
11
C#
and
Desi
gn
Patt
ern
s
ExerciseExercise