7. structure Prototype Concrete Concrete Client Clone() Clone() Clone() Operation() p = prototype->Clone() Return copy of itself Return copy of itself
8. participants Prototype Declares an instance for cloning itself Concrete Prototype Implement an operation of cloning itself Client Creates an object by asking the prototype to clone itself
9. consequences Specifying new objects by varying values. Specifying new objects by varying structure. Reduced subclassing. Configuring an application with classes dynamically. Adding and removing products at run-time.
10. applicability When a system should be independent of how its products are created, composed, and represented. When the classes to instantiate are specified at run-time 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.
15. structure ? Prototype Concrete Concrete Client Clone() Clone() Clone() Operation() p = prototype->Clone() Return copy of itself Return copy of itself
16. Factories Factory: private ConcreteA* _prototypeA; // we can have more Factory: public MakeA() { return _prototypeA->clone(); } Client: Base* obj = MakeA(); Client: obj->MoreActions();
17. Prototype managers Create and destroy prototype at run-time Register new prototypes Unregister prototypes Client retrieve and store prototype from prototype manager Use map<key,prototype>