Prototype design pattern example (c++) -


i learning prototype design pattern , confused example represented on this article sourcemaking.

class stooge { public:    virtual void slap_stick() = 0;    virtual stooge* clone() = 0; };  class larry : public stooge { public:    void slap_stick()    {       cout << "larry: poke eyes\n";    }    stooge*   clone() { return new larry; }    }; class moe : public stooge { public:    void slap_stick()    {       cout << "moe: slap head\n";    }    stooge*   clone() { return new moe; }    }; class curly : public stooge { public:    void slap_stick()    {       cout << "curly: suffer abuse\n";    }    stooge*   clone() { return new curly; } };  class factory { public:    static stooge* make_stooge( int choice ); private:    static stooge* s_prototypes[4]; };  stooge* factory::s_prototypes[] = {0, new larry, new moe, new curly};  stooge* factory::make_stooge( int choice ) {    return s_prototypes[choice]->clone(); }  int main() {    vector roles;    int    choice;     while (true)    {       cout << "larry(1) moe(2) curly(3) go(0): ";       cin >> choice;       if (choice == 0)          break;       roles.push_back(factory::make_stooge( choice ) );    }     (int i=0; < roles.size(); ++i)       roles[i]->slap_stick();    (int i=0; < roles.size(); ++i)       delete roles[i]; } 

according description prototype design pattern

  • specify kinds of objects create using prototypical instance, , create new objects copying prototype.

according this. prototype design pattern design pattern used instantiate class copying, or cloning, properties of existing object.

as far know, normal way of copying class use copy constructors, overload operator=, or implement clone function instantiate new object copying of properties of existing object.

on example above, not see how achieves creating new objects copying prototype, nor copy constructors, neither overload operator=, or appropriate clone function defined.

so can assume not implementation of prototype design pattern? or maybe wrong on assumptions , not understand example?

edited: @songyuanyao mentioned

in example, it's creating new objects newly, without copying anything

so considering above example not appropriate example of prototype pattern not represent main target of prototype pattern.

first of not necessary clone object calling method on it, possible have manager or factory have method used clone object i.e:

object factory::clone( object &){  } 

in addition, true can use copy constructor, downside of copy constructor works concrete classes. if, design decisions, offer object api (interface/ pure virtual) users, cannot use copy constructor on client side, if intended usage cloning item can resolve adding clone method

class basevirtualclass{ public:     virtual int foo() = 0;     virtual basevirtualclass * clone() = 0;     virtual ~basevirtualclass(){} };  class derivedclass: public basevirtualclass{     int state; public:     derivedclass(int a):state(a){}     derivedclass( const derivedclass & other)         : state(other.state){}     int foo(){ // override         return state;     }      basevirtualclass * clone(){ //override          // i'm using copy constructor here, hidden user          return new derivedclass( *this);     } }; 

usage:

basevirtualclass * obj = factory.createobject(); basevirtualclass * clone = obj->clone(); 

also want through factories:

basevirtualclass * obj = factory.createobject(); basevirtualclass * clone = factory.clone(obj); 

note: apart fixing missing copy constructor when offer pointers abstract classes, clone method have clear intent copy object state creating deep copy, without having write boiler plate code re-create object in particular state. when responsibility of creating object not of object (because have use factory custom allocator or other complex dependencies) clone method moved factory.


edit:

the code sample questioner found on internet seems me edge case. objects technically clones (they have no state, apart type, , hence have same state), not find example of prototype pattern:

  1. i want use prototype pattern when there complex objects interesting state needs replicated in way.
  2. the important part copying of state, internal implementation should explicit that (leaving object same state implicit may unclear internal documentation purposes)

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -