1 Prototype Design Pattern Nitin Prabhu Adapted from presentations of Mike Fortozo,John Lin

Slides:



Advertisements
Similar presentations
Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Advertisements

T O K ILL A S INGLETON F ACTORY M ETHOD P ATTERN Josh Mason 6/18/09.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Prototype8-1 Prototype CS490 Design Patterns Alex Lo, Rose-Hulman Institute May 13, 2003.
5/08 What is a Design Pattern „Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Nov, 1, Design Patterns PROBLEM CONTEXT SOLUTION A design pattern documents a proven solution to a recurring problem in a specific context and its.
Prototype Pattern Intent:
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
1 Creational Patterns CS : Software Design Winter /T8.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Type Laundering & Prototype Pattern Kunal Chaudhary.
Design Patterns.
Configuration Management and Server Administration Mohan Bang Endeca Server.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns II.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
Abstract Factory Design Pattern making abstract things.
Software Components Creational Patterns.
12/6/20041 The Factory Method Pattern Presenters 王世賀 F 陳祐毓 F 張峻銘 F 吳佩達 F 林俊成 F 鄭榮智 F 許書豪 F
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Factory Method Chris Colasuonno Also known as “Virtual Constructor”
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Advanced Object-oriented Design Patterns Creational Design Patterns.
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
The Abstract Factory Pattern (Creational) ©SoftMoore ConsultingSlide 1.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
S.Ducasse Stéphane Ducasse 1 Abstract Factory.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory pattern Intent Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
SOFTWARE DESIGN Design Patterns 1 6/14/2016Computer Science Department, TUC-N.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Generator Design Patterns: Singleton and Prototype
Abstract Factory Pattern
Factory Method Pattern
Factory Patterns 1.
Creational Pattern: Prototype
Software Design and Architecture
Software Design and Architecture
Design Patterns with C# (and Food!)
Factory Method Pattern
object oriented Principles of software design
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
What is a Design Pattern
Design Patterns Satya Puvvada Satya Puvvada.
Object Oriented Design Patterns - Creational Patterns
UNIT-III Creational Patterns UNIT-III.
Prototype Pattern 1.
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Lesson 5: More on Creational Patterns
Creational Patterns.
Presentation transcript:

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