0% found this document useful (0 votes)
2 views14 pages

29 2 Create Builder

The Builder Pattern is a creational design pattern that separates the construction of complex objects from their representation, allowing for different representations to be created using the same construction process. It involves components such as Builder, Concrete Builder, Director, and Product, which collaborate to build an object step by step. This pattern is particularly useful when the construction process must be independent of the parts that make up the object and how they are assembled.

Uploaded by

mdimberu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

29 2 Create Builder

The Builder Pattern is a creational design pattern that separates the construction of complex objects from their representation, allowing for different representations to be created using the same construction process. It involves components such as Builder, Concrete Builder, Director, and Product, which collaborate to build an object step by step. This pattern is particularly useful when the construction process must be independent of the parts that make up the object and how they are assembled.

Uploaded by

mdimberu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Builder Pattern – Creational

• Intent
Separate the construction of a complex object from
its representation so that the same construction
process can create different representations
• Motivation
» Reader for RTF (Rich Text Format) should be able
to convert to any other representation
Plain Text, MIF (Maker Interchange File),
Postscript
» Open ended number of representations possible
» Abstract the conversion process

© Gunnar Gotshalks Builder-1


Example Conversion

PLAIN TEXT + POSTSCRIPT + MIF +


CONVERTER CONVERTER CONVERTER

convert_char + convert_char + convert_char +


convert_para + convert_para + convert_para +
… … …

PLAIN_TEXT_DEF POSTSCRIPT_DEF MIF_DEF

© Gunnar Gotshalks Builder-2


Builder – Applicability

• When algorithm for creating a complex object should


be independent of the parts that make up the object
and how they are assembled

• The construction process must allow different


representations for the object that is constructed

© Gunnar Gotshalks Builder-3


Builder – Example Architecture

TEXT CONVERTER *
RTF_READER convert_char *
convert_para *

PLAIN TEXT + POSTSCRIPT + MIF +


CONVERTER CONVERTER CONVERTER

convert_char + convert_char + convert_char +


convert_para + convert_para + convert_para +
… … …

PLAIN_TEXT_DEF POSTSCRIPT_DEF MIF_DEF

© Gunnar Gotshalks Builder-4


Builder – Abstract Architecture

DIRECTOR
BUILDER *
build_part *
get_result *

+ CONCRETE_ +
CONCRETE_
BUILDER_1 BUILDER_2
PRODUCT
build_part + build_part +
get_result + get_result +

© Gunnar Gotshalks Builder-5


Builder – Participants

• Builder
Specifies abstract interface for creating parts of a
product object

• Concrete builder
Constructs and assembles parts of the product by
implementing the Builder interface

• Director
Constructs an object using the Builder interface

• Product
» The complex object under construction
» Includes classes that define the parts and interfaces for
assembling parts into a final result

© Gunnar Gotshalks Builder-6


Builder – Collaboration

• The client creates a 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

© Gunnar Gotshalks Builder-7


Builder – Scenario

Scenario: Build a product


1 create [Link]
2 [Link](aBuilder)
3 director.build_part_1
4 director.build_part_2

N director.build_part_N
N+1 director.get_product

2
CLIENT DIRECTOR
N+1
1 3 .. N
CONCRETE_BUILDER

© Gunnar Gotshalks Builder-8


Builder – Implementation

class MAZE_BUILDER
feature
build_maze is deferred end
build_room ( id : INTEGER ) is deferred end
build_door ( id1 : INTEGER ;
id2 : INTEGER ) is deferred end
maze : MAZE // builder adds to the structure
get_result : MAZE is Result := maze end
end

© Gunnar Gotshalks Builder-9


Builder – Implementation – 2

class MAZE_GAME create create_maze


feature
create_maze ( builder : MAZE_BUILDER ) is
do
the_builder := builder
builder.build_room ( 1 )
builder.build_room ( 2 )
builder.build_door ( 1 , 2 )
end

the_builder : MAZE_BUILDER
get_result : MAZE is do Result := the_builder.get_result end
end

© Gunnar Gotshalks Builder-10


Builder – Implementation – 3

class DEFAULT_MAZE_BUILDER
inherit BUILDER
feature
build_room ( id : INTEGER ) is
local room : ROOM
do
create [Link] ( id ) -- make with four walls default
maze.add_room (room )
end

...

© Gunnar Gotshalks Builder-11


Builder – Implementation – 4

build_door ( id1 : INTEGER ; id2 : INTEGER ) is


local r1 , r2 : ROOM ; door : DOOR
do
r1 := maze.get_room ( id1 )
r2 := maze.get_room ( id2 )
create [Link] ( id1, id2 )
r1.set_side ( common (r1, r2 ) , door )
r2.set_side ( common (r1, r2 ) , door )
end

end -- DEFAULT_MAZE_BUILDER

© Gunnar Gotshalks Builder-12


Builder – Implementation – 5

// Client

maze : MAZE
game : MAZE_GAME
builder : DEFAULT_MAZE_BUILDER

create builder
create game.create_maze ( builder )
maze := game.get_result

© Gunnar Gotshalks Builder-13


Builder – Related Patterns

• Abstract Factory focuses on families of product


objects, while Builder focuses on step by step
construction of complex objects

• Builder frequently builds a Composite

© Gunnar Gotshalks Builder-14

You might also like