Upcoming SlideShare
Loading in...5




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

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



Total Views
Views on SlideShare
Embed Views



1 Embed 1 1


Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Prototype Prototype Presentation Transcript

  • Prototype Cloning
  • 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
  • Intent   Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. Problem: Application "hard wires" the class of object to create in each "new" expression
    • Illustration:
    • Example
    • 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.
    • The framework also predefines a GraphicTool subclass for tools that create instances of graphical objects and add them to the document.
  • 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?
  • 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.
  • Solution Declare an abstract base class that specifies a pure virtual "clone" method, and, maintains a dictionary of all "cloneable" concrete derived classes. Any class that needs a "polymorphic constructor" 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 "new" operator on a hard-wired class name, calls a "clone" operation on the abstract base class, supplying a string or enumerated data type that designates the particular concrete derived class desired.
    • Applicability
    • Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; and
    • When the classes 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. 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.
  • Structure
  • Collaborations
    • A client asks a prototype to clone itself.
  • Consequences
    • Adding and removing products at run-time.
    • Specifying new objects by varying values.
    • Specifying new objects by varying structure.
    • Reduced subclassing.
    • Configuring an application with classes dynamically.
  • Model
  • code
    • MazePrototypeFactory::MazePrototypeFactory (
    • Maze* m, Wall* w, Room* r, Door* d
    • ) {
    • _prototypeMaze = m;
    • _prototypeWall = w;
    • _prototypeRoom = r;
    • _prototypeDoor = d;
    • }
    • Wall* MazePrototypeFactory::MakeWall () const {
    • return _prototypeWall->Clone();
    • }
    • Door* MazePrototypeFactory::MakeDoor (Room* r1, Room *r2) const {
    • Door* door = _prototypeDoor->Clone();
    • door->Initialize(r1, r2);
    • return door;
  • code
    • Door::Door (const Door& other) {
    • _room1 = other._room1;
    • _room2 = other._room2;
    • }
    • void Door::Initialize (Room* r1, Room* r2) {
    • _room1 = r1;
    • _room2 = r2;
    • }
    • Door* Door::Clone () const {
    • return new Door(*this);
    • }
  • Related Patterns
    • 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.
    • Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well.