C ++ design mode-Prototype

Source: Internet
Author: User

Purpose:
Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.

UML structure diagram:

Abstract base class:
1) prototype: virtual base class. All prototype base classes provide clone interface functions.

Interface functions:
1) prototype: clone function: pure virtual function. It is instantiated to create objects based on different Derived classes.

Resolution:
The prototype mode is actually an implementation of "virtual constructor". The implementation mechanism of C ++ does not support this feature, however, the clone interface function implemented by different Derived classes can achieve the same effect as the "virtual constructor. let's take an example to explain the role of this model. Assume that a store is equipped with a key and provides the key Preparation Service (the clone interface function is provided ), what key do you need? I don't know what kind of service it just provides. The specific key must be configured. Only the prototype that truly sees the key can be configured. that is to say, you need an object that provides this service and a prototype. Otherwise, you do not know what kind of key to configure.

Prototype is similar to the display of vehicles. When you like a car, they will give you a car of the same style, like copying in software, instead of the car at the auto show.

Example code:

// Class prototype <br/> class prototype // abstract base class <br/>{< br/> Public: <br/> Virtual Prototype * clone () = 0; <br/>}; </P> <p> // class concreteprototype1 <br/> class concreteprototype1: Public prototype <br/>{< br/> public: <br/> Virtual Prototype * clone () <br/> {<br/> concreteprototype1 * P = new concreteprototype1 (); <br/> * P = * this; // copy object <br/> return P; <br/>}< br/>}; </P> <p> // class concreteprototype2 <br/> class concreteprototype2: public prototype <br/>{< br/> Public: <br/> Virtual Prototype * clone () <br/>{< br/> concreteprototype2 * P = new concreteprototype2 (); <br/> * P = * This; // copy object <br/> return P; <br/>}< br/>}; </P> <p> // client code: </P> <p> void main () </P> <p >{</P> <p> concreteprototype1 * obj1 = new concreteprototype1 (); // prototype object 1 <br/> concreteprototype2 * obj2 = new concreteprototype2 (); // prototype object 2 </P> <p> prototype * newobj1 = obj1-> clone (); // clone object 1 <br/> prototype * newobj2 = obj2-> clone (); // clone object 2 </P> <p> // use the copied objects newobj1 and newobj2 <br/>}< br/>

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.