1 Prototype Design Pattern Nitin Prabhu Adapted from presentations of Mike Fortozo,John Lin
2 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 Manipulate() GraphicTool Manipulate() RotateTool Manipulate() Create instance ofgraphical object.Add the newlycreated instanceinto editor Graphic Staff MusicalNote WholeNoteHalfNote
3 Solution 1: Tool Manipulate() Graphic Staff MusicalNote GraphicTool Manipulate() RotateTool Manipulate() WholeNoteHalfNote StaffGraphicTool Manipulate() WholeNoteGraphicTool Manipulate()
4 Problem This approach will create may subclasses which differ only in the music objects they instantiate.
5 Example: Music Editor Tool Manipulate() Draw(Position) Clone() Staff MusicalNote GraphicTool Manipulate() RotateTool Manipulate() WholeNoteHalfNote Graphic Draw(Position) Clone() Draw(Position) Clone() Draw(Position) Clone() Return copy to self p = prototype->Clone()While (user drags mouse){ p->Draw(New Position)}insert p into drawing prototype
6 Solution 2: Prototype design pattern Tool Manipulate() Draw(Position) Clone() Staff MusicalNote GraphicTool Manipulate() RotateTool Manipulate() Graphic Draw(Position) Clone() Draw(Position) Clone() Return copy to self p = prototype->Clone()While (user drags mouse){ p->Draw(New Position)}insert p into drawing prototype
7 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.
8 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
9 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.
10 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
11 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.
12 Other Examples ClientRecord PrototypeManager Building a Maze Get(name_obj:str) “blue wall” Record “yellow wall” Record “red wall” BlueWallObj YellowWallObj RedWallObj Get(“red wall”) X <- find(name_obj);Return x.obj.clone();
13 General Structure Clone() Client Prototype Operation() ConcretePrototype1 Clone() Return copy to self ConcretePrototype2 Clone() prototype creates a new object by asking a prototype to clone itself declares an interface for cloning itself implements an operation for cloning itself
14 Thank You