SDSU CS 535 Object-Oriented Programming & Design
Spring Semester, 1999
Builder
Previous    Lecture Notes Index    Next    
© 1999, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 15-Apr-99

Contents of Doc 16, Builder


Reference

Design Patterns: Elements of Reusable Object-Oriented Software, Gamma, Helm, Johnson, Vlissides, Addison Wesley, 1995, pp. 97-105

Doc 16, Builder Slide # 2

Builder

Intent

Separate the construction of a complex object from its representation so that the same construction process can create different representations

Applicability

Use the Builder pattern when





Doc 16, Builder Slide # 3
Sample Builder Interface

class MazeBuilder {
   void buildMaze() { }
   void buildRoom( roomData ) { }
   void buildDoor( doorData ) { }
   void buildCorridor( corridorData ) { }
   // Call this when maze is complete
   Maze getMaze() { }
}
The builder know how to build mazes step by step
The builder does not know what maze you wish to build
A separate object, a director, knows the steps needed to construct a particular maze, but does not know how to build mazes or room, etc.

Doc 16, Builder Slide # 4
Collaborations

The client creates the Director object and configures it with the desired Builder object

Director notifies the builder whenever a part of the product should be built

Builder handles requests from the director and adds parts to the product

The client retrieves the product from the builder



Doc 16, Builder Slide # 5
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

Copyright ©, All rights reserved.
1999 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 15-Apr-99    Next