This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
GoF Design Pattern CategoriesPurpose
Creational Structural Behavioral
Scope Class Factory Method Adapter Interpreter
Template Method
Object Abstract Factory
Builder
Prototype
Singleton
Adapter
Bridge
Composite
Decorator
Facade
Proxy
Flyweight
Chain of Responsibility
Command
Iterator
Mediator
Memento
Observer
State
Strategy
Visitor
1
BUILDER
(Object Creational)●Intent:
Separate the construction of a complex object from
its representation so that the same construction
process can create different representations
Builders specify abstract
interfaces for creating parts
of a Product object
2
BUILDER
(Object Creational)
Usage:
Rarely used, only useful if complex objects
consisting of multiple parts need to be constructed
(composite objects for example)
3
BUILDER
(Object Creational)
●Motivation:
oRTF reader should be able to convert RTF to many
text format
oAdding new conversions without modifying the
reader should be easy
4
Builder Motivation
Each kind of converter class takes the mechanism for creating and
assembling a complex object and puts it behind an abstract
interface.
The converter is separate from the reader, which is responsible for
parsing an RTF document.
The Builder pattern captures all these relationships.
Each converter class is called a builder in the pattern, and the
reader is called the director.
5
Builder motivation
6
Builder motivation (example)
7
Builder Motivation
Applied to this example, the Builder pattern separates the
algorithm for interpreting a textual format (that is, the parser for
RTF documents) from how a converted format gets created and
represented.
This lets us reuse the RTFReader's parsing algorithm to create
different text representations from RTF documents—just
configure the RTFReader with different subclasses of
Textconverter.
8
Applicability
●Use the Builder pattern when
oThe algorithm for creating a complex object should
be independent of the parts that make up the object
and how they are assembled
oThe construction process must allow different
representations for the object that is constructed
9
Director
Construct ()
for all objects in structure
{
builder->BuildPart ()
}
Builder
BuildPart ()
ConcreteBuilder
BuildPart ()
GetResult ()
Product
builders
BUILDER
Structure
10
Builder - 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, Get-TextWidget).
11
Builder - participants
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.
12
Builder - Collaborations
●Client creates 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
13
Builder - Collaborations
14
Consequences
●It lets you vary a product's internal representation.
●It isolates code for construction and representation.
●It gives you finer control over the construction
process.
15
Implementation issues
●Assembly and construction interface.
●Why no abstract class for products?
●Empty methods as default in Builder.
16
Related Patterns
●Abstract Factory is similar to Builder in that it too
may construct complex objects.
●The primary difference is that the Builder pattern
focuses on constructing a complex object step by
step.
●Abstract Factory's emphasis is on families of product
objects (either simple or complex).
17
Related Patterns
●Builder returns the product as a final step, but as far
as the Abstract Factory pattern is concerned, the
product gets returned immediately.
●A Composite is what the builder often builds.
18
Known Uses
●RTF converter application is from ET++.
●Builder is a common pattern in Smalltalk-80.
The Service Configurator framework from the Adaptive
Communications Environment uses a builder to
construct network service components that are linked
into a server at run-time.
19
20
A scenario
You’ve just been asked to build a vacation planner for
Patternsland, a new theme park just outside of
Objectville. Park guests can choose a hotel and various
types of admission tickets, make restaurant
reservations, and even book special events.
To create a vacation planner, you need to be able to
create structures like this:
21
22
23
24
enum VehicleType
{ Car, MotorCycle}
------------------------------------------------
enum Wheels
{Wheels_2, Wheels_3, Wheels_4 }
-----------------------------------------
enum Door
{ Door_0, Door_2, Door_4}
----------------------------------
enum Frame
{ Car_Frame, MotorCycle_Frame }
-------------------------
enum Engine
{ Car_Engine_2500CC, MotorCycle_Engine_50CC} 26
class Vehicle //product
{
public Vehicle(VehicleType VType)
{ this.VType = VType; }
public VehicleType VType;
public Engine engin;
public Wheels wheels;
public Door door;
public Frame frame;
}
27
abstract class VehicleBuilder
{
protected Vehicle NewVehicle;
public Vehicle GetVehicle()
{ return NewVehicle; }
public abstract void BuildFrame();
public abstract void BuildWheels();
public abstract void BuildDoor();
public abstract void BuildEngine();
}
28
class CarBuilder:VehicleBuilder //concrete builder
{
public CarBuilder()
{ NewVehicle = new Vehicle(VehicleType.Car); }
public override void BuildDoor()
{ NewVehicle.door = Door.Door_4; }
public override void BuildFrame()
{ NewVehicle.frame = Frame.Car_Frame; }
public override void BuildWheels()
{ NewVehicle.wheels = Wheels.Wheels_4; }
public override void BuildEngine()
{ NewVehicle.engin = Engine.Car_Engine_2500CC; }
}
29
class MotorCycle:VehicleBuilder
{
public MotorCycle()
{ NewVehicle = new Vehicle(VehicleType.MotorCycle); }