The prototype model of C + + design pattern _c language

Source: Internet
Author: User

What is a prototype pattern?

In Gof's design pattern: The basics of reusable object-oriented software: Use a prototype instance to specify the type of object to create and create a new object by copying the prototypes. In this definition, the most important word is "copy", which is verbal reproduction, and this copy, which is the essence of the prototype model.

Give a simple example to illustrate the prototype pattern: Remember on primary school, the teacher to do the extra-curricular problems to write to the blackboard, and the following we have to copy these questions to their own book, go home to do, the next day to hand in, that is, each problem, the class 50 people, everyone must transcribe. According to the present time theory, it is a waste of 50 of people's time. However, at that time the conditions, the teacher is also the last resort. Now, the teacher to do an electronic version of the exercise, print a copy, and then take the printed version of the original, you can copy 50 copies.

Combined with the concept of prototype model analysis, the teacher printed out the one, 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 one.

Why use prototype mode?

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

1. It is easier to clone a new object with this type of object when our object type is not certain at the beginning, and this type is determined at runtime.

2. Sometimes, we need a copy of the object in a certain state, at this point, we use the prototype pattern is the best choice, for example: An object, after a period of processing, its internal state has changed; this time, we need a copy of this state, if directly new object, But its state is not right, at this time, you can use the prototype mode, the original object copy one out, the object and the previous object is exactly the same;

3. When we handle some relatively simple objects, and the difference between the objects is very small, may be different from a few attributes, then you can use the prototype mode to complete, eliminating the problem of creating objects;

4. Sometimes, when you create an object, the constructor has many parameters, and you do not fully know the meaning of each parameter, you can use the prototype mode to create a new object, do not have to ignore the creation process, let the creation process go to hell.
Therefore, in the above case, in the design, the appropriate consideration of the prototype model, reduce the corresponding workload, reduce the complexity of the program, improve efficiency.

Using UML class diagrams to represent prototype patterns

Since cloning requires a prototype, and prototype on this prototype in the class diagram above, prototype defines the clone interface for cloning itself, implemented by derived classes, and the implementation of the prototype pattern is focused on the implementation of this clone interface. The ConcretePrototype1 class and the ConcretePrototype2 class inherit from the prototype class and implement the Clone interface to implement the operation of cloning itself; In the ConcretePrototype1 class and the ConcretePrototype2 class, you need to override the default copy constructor for the clone function call, which is implemented by calling the overridden copy constructor internally. In the subsequent coding process, if a class needs to implement the Clone function, it simply inherits the prototype class and then overrides its own default copy constructor. As in C #, the ICloneable interface is provided, and when a class needs to implement a prototype pattern, it is just as necessary to implement the interface.

Code implementation

Copy Code code as follows:

/*
* * Filename:prototypepatterndemo
* * author:jelly Young
* * DATE:2013/11/25
* * Description:more information, http://www.jb51.net
*/

#include <iostream>
using namespace Std;

Interface
Class Prototype
{
Public:
Prototype () {}
Virtual ~prototype () {}

Virtual Prototype * Clone () = 0;
};

Realize
Class Concreteprototype:public Prototype
{
Public:
Concreteprototype (): M_counter (0) {}
Virtual ~concreteprototype () {}

Copy Constructors
Concreteprototype (const Concreteprototype & RHS)
{
M_counter = rhs. M_counter;
}

Copy yourself
Virtual Concreteprototype * Clone ()
{
Call copy constructor
return new Concreteprototype (*this);
}

Private:
int m_counter;
};

int main (int argc, char **argv)
{
Generate a pair of like
Concreteprototype * Conproa = new Concreteprototype ();

Copy yourself
Concreteprototype * Conprob = Conproa->clone ();

Delete Conproa;
Conproa= NULL;

Delete Conprob;
Conprob= NULL;

return 0;
}

The above code implements one of the simplest prototype patterns, but has already shown the basic implementation of the prototype pattern. And sometimes, when you call clone to obtain a replicated object, you need to change the state of the object, you may need to add a initialize action in the Concreteprototype class, specifically to initialize the cloned object. Because the copy constructor is invoked inside the clone, the problem with deep copy and shallow replication is also involved here. Therefore, in the actual operation process, these questions, all need to carry on the careful consideration.

Comparison with other created patterns

The factory method pattern, the abstract factory model, the builder model, and the prototype pattern are all created patterns. The factory method model is suitable for the production of more complex, when a factory produces a single product; the abstract factory pattern is suitable for a factory to produce multiple interdependent products; the builder pattern focuses on step-by-step creation of complex objects, the process of assembling products, and the creation of a process that controls the creation of each Simple object The prototype pattern is more emphasis on copying itself from itself, creating objects that are exactly the same as yourself.

Summarize

The

Prototype pattern is the most specific pattern in the creation pattern, and the specific creation process is provided by the object itself, so that we can quickly and easily build new objects in many scenarios. However, the biggest drawback of the prototype pattern is that it is difficult to inherit the subclass of the prototype to implement the clone operation. For example, it is difficult to add a clone operation when the class being considered already exists. Implementing a clone can also be difficult when the interior includes objects that do not support copying or have circular references. To say that every design pattern has its advantages and disadvantages, in the design, we need to weigh all aspects of the factors to avoid weaknesses.

Related Article

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.