Prototype Design Pattern

Download Report

Transcript Prototype Design Pattern

Prototype
Design Pattern
Nitin Prabhu
Adapted from presentations of
Mike Fortozo,John Lin
http://sern.cpsc.ucalgary.ca/~hongm/seng/609_04/design_pattern_prototype_presentation.html
http://www.cs.clemson.edu/~malloy/courses/patterns/prototype.html
1
Music Editor
GraphicTool class belongs to the framework, while the
graphical classes are application-specific, so the
GraphicTool class doesn't know how to create graphical
objects and then operate on them.
Tool
Graphic
Manipulate()
Staff
RotateTool
GraphicTool
Manipulate()
Manipulate()
MusicalNote
WholeNote
HalfNote
2
Solution 1:
Tool
Graphic
Manipulate()
Staff
RotateTool
GraphicTool
Manipulate()
Manipulate()
MusicalNote
WholeNote
StaffGraphicTool
WholeNoteGraphicTool
Manipulate()
Manipulate()
HalfNote
3
Problem
This approach will create may subclasses
which differ only in the music objects
they instantiate.
4
Example: Music Editor
Tool
Manipulate()
RotateTool
GraphicTool
Manipulate()
Manipulate()
prototype
Graphic
Draw(Position)
Clone()
Staff
Draw(Position)
Clone()
MusicalNote
WholeNote
Draw(Position)
Clone()
HalfNote
Draw(Position)
Clone()
5
Solution 2: Prototype design pattern
Tool
Manipulate()
RotateTool
GraphicTool
Manipulate()
Manipulate()
prototype
Graphic
Draw(Position)
Clone()
Staff
Draw(Position)
Clone()
MusicalNote
Draw(Position)
Clone()
6
Solution 2:
To create a new graphical object, the
prototypical instance of the specific graphical
class is passed as parameter to the
constructor of the GraphicTool class.
The GraphicTool class can then use this
prototype to clone a new object and operate
on it.
Therefore, if all the Graphic subclasses
support a Clone operation, then the
GraphicTool Class can clone any kind of
Graphic.
7
Solution 2:
Prototype Pattern can also be used to reduce the
number of classes.
The WholeNote and HalfNote class only differ in that
they have different bitmaps and durations.
Instead of defining a subclass of MusicalNote class
for each type of notes, we can make the MusicalNote
class concrete, and the note objects can be instances
of the same MuscialNote class initialized with
different setting.
Therefore, for GraphicTool class, to create a new
WholeNote object is to clone a prototype that is
MuscialNote object initialized to be a WholeNote
8
Applicability
When the classes to instantiate are specified at
run-time
When a system should be independent of how its
products are created, imposed, and represented.
When to avoid building a class hierarchy of
factories that parallels the class hierarchy of
products
When instances of a class can have one of only a
few different combinations of state.
9
Consequences
Adding and removing products at run-time
Specifying new objects by varying structure
Specifying new objects by varying values
Reduced subclassing
Configuring an application with classes dynamically
10
Implementation Issues
Using a Prototype Manager
- keep a registry of available prototypes
Implementation the Clone operation
- the hardest part of the prototype pattern
Initializing clones
- only if the clone needs to be initialized
with specific parameter.
11
Other Examples
• Building a Maze
Client
Record
BlueWallObj
“blue wall”
Get(“red wall”)
Record
YellowWallObj
“yellow wall”
PrototypeManager
Record
Get(name_obj:str)
“red wall”
RedWallObj
12
General Structure
Prototype
Clone()
prototype
Client
Operation()
ConcretePrototype1
ConcretePrototype2
Clone()
Clone()
13
Thank You
14