billy bennett june 22, 2009. intent specify the kinds of objects to create using a prototypical...
TRANSCRIPT
![Page 1: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/1.jpg)
Billy BennettJune 22, 2009
![Page 2: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/2.jpg)
![Page 3: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/3.jpg)
Intent• Specify the kinds of objects to create using
a prototypical instance, and create new objects by copying this prototype
![Page 4: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/4.jpg)
Applicability• System must be independent of how products
are created, composed, and represented (usually true of Creational patterns)
AND• Classes to instantiate are specified at run-time• To avoid building a class hierarchy of factories
for a class hierarchy of products• Instances of classes have only a few different
combinations of state
![Page 5: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/5.jpg)
Structure
![Page 6: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/6.jpg)
Participants• Prototype – declares an interface for cloning
itself• ConcretePrototype – implements an
operation for cloning itself• Client – creates a new object by asking a
prototype for a clone of itself
![Page 7: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/7.jpg)
Consequences – GOOD• Can add/remove products at run time• Can specify new objects by varying values• Can specify new objects by varying
structure• Less subclassing – why is everyone so down
on inheritance?• Configuring an application with classes
dynamically
![Page 8: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/8.jpg)
Consequences – BAD• Every subclass of Prototype must
implement the Clone operation Can be difficult when classes to be constructed
already exist Can be difficult when internal objects within a
Prototype can’t be copied or have circular references
![Page 9: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/9.jpg)
Implementation Tips• Using a prototype manager• Implementing the Clone operation• Initializing Clones
![Page 10: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/10.jpg)
Related Patterns• Often used with Composite or Decorator• Abstract Factory
May compete with Prototype May store a set of Prototypes
![Page 11: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/11.jpg)
![Page 12: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/12.jpg)
To Start:• We have an abstract base class “Event”• Event has few operations
virtual long timestamp() = 0; (time of event) virtual const char* rep() = 0; (packet of data)
• The problem: no universal interface to define events A vending machine would need CoinType() and
CoinReturn(), etc.
![Page 13: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/13.jpg)
The Questions:• 1. How does the framework create instances
of domain-specific subclasses?• 2. How does the application code access
subclass-specific operations, when all it gets is an Event object?
![Page 14: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/14.jpg)
The Questions:• 1. How does the framework create
instances of domain-specific subclasses? How about Prototype? Add an operation to the base Event class:
virtual Event* copy() Can be an instance of any Event subclass
![Page 15: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/15.jpg)
The Questions:• 2. How does the application code access
subclass-specific operations, when all it gets is an Event object? We could just dynamic_cast over and over…Yuck We could use Visitor… add void accept(EventVisitor&) to base Event
class But then we have to define a bunch of domain-
specific events within the Visitor class…Yuck (just like dynamic casting)
![Page 16: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/16.jpg)
Another solution – Memento• Implement a cursor (a la Iterator) as a
Memento itself• “Type Laundering” – define a cursor base
class that indicates which aspects of the cursor should be public (e.g. the destructor)
![Page 17: Billy Bennett June 22, 2009. Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype](https://reader035.vdocument.in/reader035/viewer/2022070415/5697bfc91a28abf838ca8b70/html5/thumbnails/17.jpg)