/** * prototype.h * Implemented by Blueprint Technologies, Inc. */ #ifndef _prototype_h #define _prototype_h /** * Declares an interface for cloning itself. */ class Prototype { public: virtual Prototype* clone() = 0; }; /** * Implements an operation for cloning itself. */ class ConcretePrototype1: public Prototype { private: int setting; public: virtual Prototype* clone() { ConcretePrototype1* copy = new ConcretePrototype1(); copy -> setSetting( getSetting() ); return (Prototype *) copy; }; /** * Accessors specific to this class. */ int getSetting() { return setting; }; void setSetting( int setting ) { this -> setting = setting; }; }; /** * Implements an operation for cloning itself. */ class ConcretePrototype2: public Prototype { private: Prototype* aggregate; public: ConcretePrototype2(): Prototype(), aggregate(0) { }; virtual Prototype* clone() { ConcretePrototype2* copy = new ConcretePrototype2(); if( getAggregate() ) { copy -> setAggregate( getAggregate() -> clone() ); } return (Prototype *) copy; }; Prototype* getAggregate() { return aggregate; }; void setAggregate( Prototype* aggregate ) { this -> aggregate = aggregate; }; }; /** * Creates a new object by asking a prototype to clone itself. */ class Client { private: Prototype* prototype; public: void operation() { Prototype* copy = prototype -> clone(); /** * Do something with the clone. */ delete copy; }; Prototype* getPrototype() { return prototype; }; void setPrototype( Prototype* prototype ) { this -> prototype = prototype; }; }; #endif