Prototype [PDF]

new Bombed Wall(), new Room With Bomb (), new Door());. Maze maze1 = s Game. Create Maze (simple Maze Factory);. Maze ma

3 downloads 5 Views 102KB Size

Recommend Stories


prototype
Don't ruin a good today by thinking about a bad yesterday. Let it go. Anonymous

Prototype 2017
At the end of your life, you will never regret not having passed one more test, not winning one more

prototype report
Before you speak, let your words pass through three gates: Is it true? Is it necessary? Is it kind?

Paper Prototype
Make yourself a priority once in a while. It's not selfish. It's necessary. Anonymous

prototype theory
Live as if you were to die tomorrow. Learn as if you were to live forever. Mahatma Gandhi

Permafrost Ecosystem Warming Prototype
You often feel tired, not because you've done too much, but because you've done too little of what sparks

Nearest Prototype Classification
Why complain about yesterday, when you can make a better tomorrow by making the most of today? Anon

(SEEA) Prototype and Roadmap
Happiness doesn't result from what we get, but from what we give. Ben Carson

Prototype Theory & the Enneagram
If your life's work can be accomplished in your lifetime, you're not thinking big enough. Wes Jacks

Prototype JavaScript Library 1.5.0
If you feel beautiful, then you are. Even if you don't, you still are. Terri Guillemets

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

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.