Sie sind auf Seite 1von 12

1

BUILDER
Bhavana
Topics
Builder Definition & Structure
RTF Reader Example
Participants
Collaborations
Advantages
Related Patterns
Conclusions
2
What is a Builder?
Def: Builder is an object creational design pattern that
codifies the construction process outside of the actual
steps that carries out the construction - thus allowing
the construction process itself to be reused.
More
Builder focuses on constructing a complex object step by step.
Build a complex object in a specified way, from different
concrete classes. E.g.: An airplane has wings, tails, engines,
gears, etc,. all selected from families of components.
The client uses the abstract factory class methods to create its
own object, in Builder the client instructs the builder class on
how to create the object and then asks it for the result.
Builder returns the product as a final step.
Builder is to creation as Strategy is to algorithm.
3
What more
Builder is used to isolate the actual implementation of how to build a
complex object, so that the construction procedure can take place
for different types of objects without having to wary about the details
of how different objects are constructed.
Builder has the factory object building a product incrementally, and
the factory object provides a complex protocol for producing its
product, which is usually a complex object.
INTENT:
Separates the construction of a complex object from its
representation so that the same construction process can create
different representations.
Non- software Example:
4
HDD
FDD
Monitor
|
|
|
|
|
|
|
|
Builder Structure
5
Builder Pattern
Builder pattern allows a client object to construct a complex
object by specifying only its type and content. The client is
unaware of the object construction.
It is a pattern for step-by-step creation of a complex object
so that the same construction process can create different
representations is the routine in the builder pattern that also
makes for finer control over the construction process.
Builder Class :
Its an abstract interface for a construction process that
puts together a system of specified type.
Builder :The Builder defines the methods for building parts of a Product. There is one
method for each part that need to be created.
Client :Client create a Builder and a Director and use them in conjunction to build a
Product.
ConcreteBuilder: It provides the means to build Concrete Product instances. Each
ConcreteBuilder builds a different data representation by constructing and assembling
parts of a product.
ConcreteProduct: The actual product built by the ConcreteBuilder. The
ConcreteProduct differs between ConcreteBuilder extensions.
Director: The Director(or controller) executes a sequence of Builder call in order to build
a Product.
Product: The result of the Director executing a script. The Product represents the
complex object being built.
6
RTF Reader Example
Builder Patterns : Participants
7
Builder Pattern : Collaborations
The Client creates the controller
object and configures it with the
desired Builder object.
Controller notifies the Builder
whenever a part of the object should
be built.
Builder handles requests from the
Controller to build up the
representation.
The Client retrieves the result of the
build from the Builder Object.
Example
8
C++ Implementation
//Creational Pattern: BUILDER
using System;
class Director
{
public void Construct(IBuilder builder)
{
builder.DoIt();
}
}
interface IBuilder
{
void DoIt();
}
class BuilderA : IBuilder
{
public void DoIt()
{
//Necessary code for building the computer type A
Console.WriteLine("Assembly a Computer with mono monitor");
}
}
class BuilderB : IBuilder
{
public void DoIt()
{
//Necessary code for building the computer type B
Console.WriteLine("Assembly a Computer with color monitor");
}
}
class MyClient
{
public static void Main()
{
Director d = new Director();
IBuilder build = new BuilderA();
d.Construct(build);
}
}
Creational Patterns
9
Related Patterns
Abstract Factory Builder :
~ Both construct data or object structures.
Builder constructs the object step-by-step and the
result is requested at a later stage. The Abstract
Factory returns the requested object immediately.
A Composite is what a Builder often builds.
A Builder is a Strategy that is specialized to create a
Composite object or data structure.
When shouldnt be used?
If the Interface is not stable then the Builder has few
benefits.
Every interface change requires a change to the
Controller and impacts the abstract base class or its
objects.
~ Addition of a new method.
~ Changes in a specific method Interface
Change in the base class
and all concrete classes that
will override the new method
A specific method interface
change would require all concrete
classes supporting the old method
to change.
10
When to Use?
The Process of constructing an object must support
different representations of the constructed objects.
The construction of an object is complex and should be
encapsulated and localized in a separate class.
Advantages
Encapsulation: The builder pattern encapsulates the construction
process of a complex object and thereby increases the modularity of
an application.
A builder lets you vary an objects representation without changing
the way the object is constructed.
Give control over the build process.
11
Conclusions
Builder patterns is useful in a large system.
Builder patterns fits when different variations of an object
may need to be created and the inheritance into those
objects is well-defined.
References
Design Patterns : Elements of reusable E.Gamma, R.Helm, R.Johnson,
J.Vlissides
http://ei.cs.vt.edu/~cs6704/RealWorld.ppt
http://www.engr.mun.ca/~theo/Courses/sd/pub/sd10.pdf
http://www.cse.unl.edu/~fayad/current.courses/csce966/notes/SWA-FW-
L15V2.ppt
http://www.cs.clemson.edu/~malloy/courses/patterns/patterns.html
http://patterndigest.com/patterns/Builder.html
http://www.cs.pdx.edu/~antoy/Courses/Patterns/slides/Creational.html
12
Questions

Das könnte Ihnen auch gefallen