Design pattern--Prototype mode C + + implementation

Source: Internet
Author: User
Prototype Mode C + + implementation 1 definition

Specify the kind of objects created with the prototype instance and create new objects by copying the prototypes

2 class Diagram

3 implementation

Class Prototype

{

Protected

Prototype ();

Public

Virtual prototype* Clone () const = 0;

Vitual ~prototype () = 0;

};

Class Prototypeclass:public Prototype

{

Public

Prototypeclass ();

Prototypeclass (const PROTOTYPECLASS & CP);

prototype* Clone () const

{return 0;}

};

Note: The key to the prototype model in C + + is the implementation of the copy constructor.

Factory mode, builder mode, and prototype mode all construct new objects through existing class objects, except

Factory mode: Focus on mass production of multiple interdependent classes of objects

Builder mode: Step-up creation with emphasis on complex objects

Prototype mode: Implement new object construction by copying itself

4 applications

Advantages

Good performance, avoid the constraints of the constructor function.

5 Usage Scenarios

① Resource optimization scenario, class initialization requires a large resource (data, hardware)

② Performance and security requirements, data preparation and permissions issues with new frequent production

③ an object with multiple modified scenes, eg student Information form.

6 precautions

Constructor will not be executed

A deep copy problem, when the inside of a class contains pointers, it is necessary to implement a dark copy yourself.

Design pattern--Prototype mode C + + implementation

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.