Prototype Cloning
Prototype The  Prototype  pattern specifies the kind of objects to create using a prototypical instance. Prototypes of new...
Intent    Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this pro...
<ul><li>Illustration: </li></ul><ul><li>Example </li></ul><ul><li>In the figure the framework provides an abstract Graphic...
Illustration:Problem Here the  Problem is  the GraphicTool, ie the classes for notes and staves are specific to our applic...
Illustration: Solution   The solution lies in making GraphicTool create a new Graphic by copying or “cloning” an instance ...
Solution   Declare an abstract base class that specifies a pure virtual &quot;clone&quot; method, and, maintains a diction...
<ul><li>Applicability </li></ul><ul><li>Use the Prototype pattern when a system should be independent of how its products ...
Structure
Collaborations <ul><li>A client asks a prototype to clone itself.   </li></ul>
Consequences <ul><li>Adding and removing products at run-time. </li></ul><ul><li>Specifying new objects by varying values....
Model
code <ul><li>MazePrototypeFactory::MazePrototypeFactory  ( </li></ul><ul><li>Maze* m, Wall* w, Room* r, Door* d </li></ul>...
code <ul><li>Door::Door  (const Door& other) { </li></ul><ul><li>_room1 = other._room1; </li></ul><ul><li>_room2 = other._...
Related Patterns <ul><li>Prototype and Abstract Factory are completing patterns in some ways, as we discuss at the end of ...
Upcoming SlideShare
Loading in …5
×

Prototype

674 views
639 views

Published on

Object-oriented analysis and design(OOAD) and Design Pattern Slides UML Slides. for more slides refer www.scmGalaxy.com

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
674
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
32
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Prototype

  1. 1. Prototype Cloning
  2. 2. Prototype The Prototype pattern specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full production, but in this example, the prototype is passive, and does not participate in copying itself. The mitotic division of a cell, resulting in two identical cells, is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotype result. In other words, the cell clones itself. Object Diagram for Prototype using Cell Division Example
  3. 3. Intent   Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. Problem: Application &quot;hard wires&quot; the class of object to create in each &quot;new&quot; expression
  4. 4. <ul><li>Illustration: </li></ul><ul><li>Example </li></ul><ul><li>In the figure the framework provides an abstract Graphic class for graphical components, like notes and staves. Moreover it will provide an abstract tool class for defining tools like those in the palette. </li></ul><ul><li>The framework also predefines a GraphicTool subclass for tools that create instances of graphical objects and add them to the document. </li></ul><ul><li> </li></ul>
  5. 5. Illustration:Problem Here the Problem is the GraphicTool, ie the classes for notes and staves are specific to our application, but the GraphicTool class belongs to the framework. The GraphicTool doesn’t know how to create instances of our music classes to add to the score. The objective is to minimize the height of the Client (GraphicTool) Class Hierarchy given the prototype hierarchy.   The question is , how can the framework use the Graphic to parameterize instances of GraphicTool by the class of Graphic they’re supposed to create?
  6. 6. Illustration: Solution The solution lies in making GraphicTool create a new Graphic by copying or “cloning” an instance of a Graphic subclass. We call this instance a prototype.
  7. 7. Solution Declare an abstract base class that specifies a pure virtual &quot;clone&quot; method, and, maintains a dictionary of all &quot;cloneable&quot; concrete derived classes. Any class that needs a &quot;polymorphic constructor&quot; capability: derives itself from the abstract base class, registers its prototypical instance, and implements the clone() operation. The client then, instead of writing code that invokes the &quot;new&quot; operator on a hard-wired class name, calls a &quot;clone&quot; operation on the abstract base class, supplying a string or enumerated data type that designates the particular concrete derived class desired.
  8. 8. <ul><li>Applicability </li></ul><ul><li>Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; and </li></ul><ul><li>  </li></ul><ul><li>When the classes to instantiate are specified at run-time, for example, by dynamic loading; or </li></ul><ul><li>  </li></ul><ul><li>To avoid building a class hierarchy of factories that parallels the class hierarchy of products; or </li></ul><ul><li>  </li></ul><ul><li>When instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state. </li></ul><ul><li>  </li></ul>
  9. 9. Structure
  10. 10. Collaborations <ul><li>A client asks a prototype to clone itself. </li></ul>
  11. 11. Consequences <ul><li>Adding and removing products at run-time. </li></ul><ul><li>Specifying new objects by varying values. </li></ul><ul><li>Specifying new objects by varying structure. </li></ul><ul><li>Reduced subclassing. </li></ul><ul><li>Configuring an application with classes dynamically. </li></ul>
  12. 12. Model
  13. 13. code <ul><li>MazePrototypeFactory::MazePrototypeFactory ( </li></ul><ul><li>Maze* m, Wall* w, Room* r, Door* d </li></ul><ul><li>) { </li></ul><ul><li>_prototypeMaze = m; </li></ul><ul><li>_prototypeWall = w; </li></ul><ul><li>_prototypeRoom = r; </li></ul><ul><li>_prototypeDoor = d; </li></ul><ul><li>} </li></ul><ul><li>Wall* MazePrototypeFactory::MakeWall () const { </li></ul><ul><li>return _prototypeWall->Clone(); </li></ul><ul><li>} </li></ul><ul><li>Door* MazePrototypeFactory::MakeDoor (Room* r1, Room *r2) const { </li></ul><ul><li>Door* door = _prototypeDoor->Clone(); </li></ul><ul><li>door->Initialize(r1, r2); </li></ul><ul><li>return door; </li></ul>
  14. 14. code <ul><li>Door::Door (const Door& other) { </li></ul><ul><li>_room1 = other._room1; </li></ul><ul><li>_room2 = other._room2; </li></ul><ul><li>} </li></ul><ul><li>void Door::Initialize (Room* r1, Room* r2) { </li></ul><ul><li>_room1 = r1; </li></ul><ul><li>_room2 = r2; </li></ul><ul><li>} </li></ul><ul><li>Door* Door::Clone () const { </li></ul><ul><li>return new Door(*this); </li></ul><ul><li>} </li></ul>
  15. 15. Related Patterns <ul><li>Prototype and Abstract Factory are completing patterns in some ways, as we discuss at the end of this chapter. They can also be used together, however. An abstract Factory might store a set of prototypes from which to clone and return product objects. </li></ul><ul><li>  </li></ul><ul><li>Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well. </li></ul>

×