Idea Transcript
DESIGN PATTERNS PROTOTYPE
Presented By: Narendranadh.J
INTENT • Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype
1
EXAMPLE SCENARIO Graphic Draw (position) Clone ()
Tool Manipulate() prototype Rotate Tool Manipulate()
Graphic Tool Manipulate() P=prototype->clone() While (user drags mouse) { P->draw (new position) } Insert p into drawing
Staff Draw (Position) Clone() Whole Note Draw (position) Clone()
Musical Note
Half Note Draw (Position) Clone ()
Return copy of self Return copy of self
APPLICABILITY • When the class to instantiate are specified at run-time, for example, by dynamic loading; or • To avoid building a class hierarchy of factories that parallels the class hierarchy of products; or • When instances of a class can have one of only a few different combinations of state
2
STRUCTURE Prototype Clone()
Client Operation()
P=prototype->Clone()
Concrete Prototype1
Concrete Prototype2
Clone()
Clone()
Return copy of self
Return copy of self
PARTICIPANTS • Prototype - declares an interface for cloning itself • Concrete Prototype - implements an operation for cloning itself • Client - creates a new object by asking a prototype to clone itself
3
INTERACTION DIAGRAM
Source: Prototype Design Pattern, SENG 609.04,By Elena Fanea and David Baker
COLLABORATIONS • A client asks a prototype to clone itself
4
CONSEQUENCES • Hides the concrete product classes from the client • Lets a client work with application-specific classes without modification Additional Benefits are: • Adding and removing products at run-time • Specifying new objects by varying values • Specifying new objects by varying structure • Reduced sub classing • Configuring an application with classes dynamically
IMPLEMENTATION • Using a prototype manager • Implementing the clone operation - Shallow copy Versus deep copy? • Initializing clones
5
SAMPLE CODE
public class Maze Factory { public Maze make Maze() { return new Maze(); } public Wall make Wall() { return new Wall(); } public Room make Room (int room Number) { return new Room (room Number); } public Door make Door (Room room1, Room room2) { return new Door(room1, room2); } }
SAMPLE CODE Public class Maze Game { Maze Create Maze (Maze Factory fact) { Maze m = fact. Make Maze (); Room r1=fact. Make Room (); Room r2=fact. Make Room (); Door d=fact. Make Door (); m. add Room (r1); m. add Room (r2); r1. set Side ( north, fact. make Wall()); r1. set Side ( east, d); r1. set Side ( south, fact. make Wall()); r1. set Side ( west, fact. make Wall()); r2. set Side ( north, fact. make Wall()); r2. set Side ( east, fact. make Wall()); r2. set Side ( south, fact. make Wall()); r2. set Side ( west, d); return m; } }
6
SAMPLE CODE Public Class MazePrototypeFactory extends Maze Factory { private Maze prototype Maze; private Maze prototype Room; private Maze prototype Wall; private Maze prototype Door; public Maze Prototype Factory ( Maze m, Wall w, Room r, Door d) { prototype Maze = m; prototype Room = r; prototype Wall = w; prototype Door = d; }
SAMPLE CODE Public Maze Make Maze() { return (Maze) prototype Maze. clone(); } Public Room Make Room (int room No) { return (Room) prototype Room. Clone(); } Public Wall Make Wall() { return (Wall) prototype Wall. Clone(); } Public Door Make Door (Room r One, Room r Two) { return (Door) prototype Door. Clone(); }
7
Public static void main (String a []) { Maze Game s Game= new Maze Game(); Maze Game b Game= new Maze Game(); Maze Prototype Factory simple Factory = new Maze Prototype Factory ( new Maze(), new Wall(), new Room(), new Door()); Maze Prototype Factory bombed Maze Factory = new Maze Prototype Factory (new Maze(), new Bombed Wall(), new Room With Bomb (), new Door()); Maze maze1 = s Game. Create Maze (simple Maze Factory); Maze maze2 = b Game. Create Maze (bombed Maze Factory); }// main } // class Maze Prototype Factory
REFERENCES •Design Patterns: Elements of Reusable Object Oriented Software by Erich Gamma etal.
8