10. [C + +] prototype mode

Source: Internet
Author: User

The problem solved:

Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.

This is in fact the role of the copy constructor of C + + is consistent, in fact, the dynamic extraction of the current object runtime state (well, I have not yet understood the difference between the two, please Daniel Enlighten).

The copy constructors of C + + are divided into shallow and deep copies.

Shallow copy: It is to copy each member variable in the object, which is to assign the variables in the A1 class directly to the variables in the A2 class, which is the value passing, but involves the pointer type, they point to the same piece of memory. There is a problem: when B releases the memory (for example, a destructor), a pointer inside a is a wild pointer, and a run error occurs.

Deep copy: It is not only the use of value to pass, but each variable has its own independent memory space, non-interference.

Prototype mode UML class diagram:

The following code is excerpted from: "C + + Design mode-prototype mode"

/** * filename:prototypepatterndemo** author:jelly young** date:2013/11/25** description:more Information, go tohttp://www.jellythink.com*/#include<iostream>using namespacestd;//Interfaceclassprototype{ Public: Prototype () {}Virtual~Prototype () {}VirtualPrototype * Clone () =0;};//ImplementclassConcreteprototype: Publicprototype{ Public: Concreteprototype (): M_counter (0){}    Virtual~Concreteprototype () {}//copy ConstructorConcreteprototype (ConstConcreteprototype &RHS) {M_counter=RHS. M_counter; }    //copying itself    VirtualConcreteprototype *Clone () {//Call copy Constructor        return NewConcreteprototype (* This ); }Private :    intM_counter;};intMainintARGC,Char**argv) {    //generate a pair of imagesConcreteprototype * Conproa =NewConcreteprototype (); //copying itselfConcreteprototype * Conprob = conproa->Clone ();    Delete Conproa; Conproa=NULL;    Delete Conprob; Conprob=NULL; return 0;}

Common scenarios

Basically, you need to use prototype mode to get an instance of a that is the same as a content, but does not interfere with each other.

For example (may be wrong ...):

When we play a copy of the game, some games are designed to save the current character state before the copy, and then restore the state of the forward copy after the copy is finished, is it possible to apply the prototype mode at this time? Copy an instance of the current role before the copy, and then use the copied new instance to maintain the role after the copy is made, and the old instance is used when the copy is exited ...

10. [C + +] prototype mode

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.