The application of design pattern in game--prototype mode (VI)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.