Prototype Mode--prototype

Source: Internet
Author: User

C + + design mode-prototype modeWhat is prototype mode?

In Gof's design pattern: The basics of reusable object-oriented software, this is said: Use prototype instances to specify the kind of objects that are created, and create new objects by copying those prototypes. In this definition, the most important word is "copy", which is the verbal copy, and this copy, which is the essence of the prototype pattern.

Give a simple example to illustrate the prototype model: Remember when the primary school, the teacher to do the extra-curricular exercises to write to the blackboard, and the following we have to copy these questions to their own book, go home, the next day to hand in, that is, each problem, the class 50 people, everyone to copy it again. According to the current theory of time, it is a waste of 50 people's time. But, at that time, the teacher was forced to do so. Now, the teacher to do an electronic version of the exercise, print a copy, and then take this printed version of the original, you can copy out 50 copies.

Combining the concept of prototype model analysis, the teacher printed out of the copy, is "prototype", and the copy of the 50 copies, is the use of "copy." And the prototype model is such a simple truth, through the existing things, and then copied out one.

Why use prototype mode?

Prototype mode is one of the creation patterns, as is the builder mode and factory method pattern. In a nutshell, we use prototype mode to create objects. However, using prototype mode is the best option in the following scenarios:

    1. It is easier to clone a new object from this type of object when our object type is determined at the beginning of the operation, and this type is determined at runtime.
    2. Sometimes, we need a copy of an object in a certain state, at this point, we use the prototype mode is the best choice, for example: An object, after a period of processing, the state of its internal changes; this time, we need a copy of this state, if it is directly new to the object, But its state is not right, at this time, you can use the prototype mode, the original object is copied out, the object is exactly the same as the previous object;
    3. When we are dealing with some relatively simple objects, and the difference between objects is very small, maybe a few properties are different, then you can use the prototype mode to complete, eliminating the trouble of creating objects;
    4. Sometimes, when creating an object, the constructor has a lot of arguments, and you do not fully know the meaning of each parameter, you can use the prototype pattern to create a new object, do not have to ignore the creation process, let the creation process to hell.

Therefore, in the above circumstances, in the design, the appropriate consideration of the prototype model, reduce the corresponding workload, reduce the complexity of the program, improve efficiency.

Representing prototype patterns with UML class diagrams

Since cloning requires a prototype, and the above class diagram prototype this prototype, prototype defines the clone's own clone interface, which is implemented by the derived class, and the key to implementing the prototype pattern is the implementation of the Clone interface. The ConcretePrototype1 class and the ConcretePrototype2 class inherit from the prototype class and implement the Clone interface to implement the cloning operation; In the ConcretePrototype1 class and the ConcretePrototype2 class, you need to override the default copy constructor for the clone function call, and clone is implemented by calling the overridden copy constructor internally. In the subsequent encoding process, if a class needs to implement the Clone function, it is only necessary to inherit the prototype class and then rewrite its own default copy constructor. As with the ICloneable interface provided in C #, when a class needs to implement a prototype schema, the only way to implement this interface is the same.

Code implementation
/*** filename:prototypepatterndemo** author:jelly young** date:2013/11/25** Description:more in Formation, go to*/#include <iostream>Using NamespaceStd;InterfaceClass Prototype{Public :Prototype(){}Virtual ~Prototype(){}Virtual Prototype * Clone() = 0;};RealizeClass Concreteprototype : Public Prototype{Public :Concreteprototype():M_counter(0){}Virtual ~Concreteprototype(){}Copy constructorConcreteprototype( Const Concreteprototype &Rhs){M_counter=Rhs.M_counter;}Copying itselfVirtual Concreteprototype * Clone(){Call copy constructorReturn New Concreteprototype (*This );}Private :IntM_counter;};IntMain(Intargc, Char **Argv){Generate a pair of imagesConcreteprototype *Conproa= New Concreteprototype ();//copy itself concreteprototype * Conprob = Conproa- >clone (); delete Conproa;= NULL ;delete Conprob;= NULL ;return 0;                /span>                

The code above implements one of the simplest prototype patterns, but has already shown the basic implementation principle of the prototype pattern. Sometimes, when you call clone to get a copy of the object, you need to change the state of the object, you may need to add a initialize operation in the Concreteprototype class, specifically to initialize the cloned object. Because the copy constructor is called Inside clone, there is a problem with deep and shallow replication. Therefore, in the actual operation of the process, these problems need to be carefully considered.

Comparison with other creation patterns

Factory method mode, abstract Factory mode, builder mode, and prototype mode are all created patterns. The factory method model is suitable for the production of more complex, a factory to produce a single product of the time; abstract Factory mode is suitable for a factory to produce multiple interdependent products; the builder model focuses on the step-by-step creation of complex objects, the process of assembling the product, and the creation of a simple object that can be controlled during creation The prototype pattern is more emphasis on copying itself from itself, creating objects that are identical to themselves.


The prototype pattern is the most special pattern in the creation pattern, the concrete creation process is provided by the object itself, so we can construct the new object quickly and easily in many scenes. However, the biggest disadvantage of the prototype pattern is that the subclass that inherits the prototype has to implement the clone operation, which is very difficult. For example, it is difficult to add a clone operation when the class being considered already exists. Cloning can also be difficult when the internals include objects that do not support copying or have circular references. Said to say, each design pattern has its advantages and disadvantages, in the design, we need to weigh all aspects of factors, weaknesses.

Prototype Mode--prototype

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: 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.