Prototype prototype mode is a creation design pattern, prototype mode allows an object to create another customizable object without knowing the details of how to create it, by passing a prototype object to the object to be created, The objects to be created are created by requesting the prototype object to copy them themselves.
Above is a UML structure diagram of the prototype schema.
Here is the code for the prototype pattern:
#include "stdafx.h"#include <iostream>#include <string>usingNamespace Std;class iclone{ Public:Iclone(){};Virtual~iclone () {}; Public:Virtualiclone*Clone() =0;}; Class Player: Publiciclone{ Public:Player() {m_name ="Test"; m_id =1; }; iclone* Clone () {player* ret =NewPlayer (); Ret->setname (M_name);returnRet }voidSetName (stringName) {m_name = name; }stringGetName () {returnM_name; }voidSetId (intID) {m_id = ID; }intGetId () {returnm_id; }Private:stringM_name;intm_id;};int_tmain (intARGC, _tchar* argv[]) {player* Player1 =NewPlayer (); Player1->setid (3); Player1->setname ("Cloneobject"); player* player2 = (player*) player1->clone (); Cout<<player2->getname () <<endl; Cout<<player2->getid () <<endl;return 0;}
The output results are as follows:
The prototype model belongs to a build pattern, in fact, very similar to the copy constructor, I have always felt that the prototype model is completely useless, but as the code is much more, it is found that the prototype model resembles a snapshot of the object at some point, and the copy construction of the previous article is completely different.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The application of design pattern in game--prototype mode (VI)