Design Pattern (7): Prototype-creation Pattern

Source: Internet
Author: User
Tags net serialization

Prototype

 

Important statement:

The story is purely fictitious. If there are similarities, please do not sit in the seat. The story is just for reference. Although it starts with the first person and the author's net name, it does not represent my real thoughts, do not associate the characters in the story with the author. The intention of the story is only to create preconditions for the model.

Plot:

Every morning, I open my eyes and wash my eyes. I have a cup of milk tea and two egg yolk pies. After I have screwed up my bags, I stepped onto work. After a day, I dragged my tired body back, I don't want to get up again when I sit down in bed, so I picked up my notebook and wrote my own code or read a book. When I got tired, I felt like it was dawn. the programmer's day is so boring and boring. Every day, he repeats the same thing, boring, just like cloning it.

I had a hard time and thought back to the previous week's breakfast life. It turned out to be a very good kind of epitome: 1 cup of milk, 2 iombies, and I eat it every morning, the iombies look like a taste. In fact, some common sense friends know that the iombies are chicken cakes with cream. They are all cloned one by one with abrasive tools, so they look like the same in general. Mayonnaise has many flavors including strawberry with cream, fresh orange, and so on. So are there many egg factories that want to produce all kinds of egg tools. The answer is no! It is impractical to change the production pipeline for every kind of egg yolk pie in the factory. In fact, it is very easy to add the containers injected with cream to other flavors of cream and then completely clone other programs.


Because this mode is very simple, you do not need to apply the previous design pattern principles. This mode is very simple. The theme is clone. I personally think it is the simplest production mode after the single-piece mode. The mode is simple, but its actual operations are not that simple. The important technical points of prototype are the problems of pre-copy and deep copy.

(The following is an excerpt from. NET Framework programming (revised version), translated by Li Jianzhong). When a field value of an object is copied, the object referenced by the field is not copied. For example, if an object has a field pointing to a string and we make a shortest copy of the object, the two objects will reference the same string. Deep copy is also a method for copying objects referenced by fields in the object instance. Therefore, if an object has a field pointing to a string, if we make a deep copy of the object, we will create a new object and a new string-the new object will reference the new string. Note that after deep copy, the original object and the newly created object will not share anything; changing an object will not affect the other object.

. Net COPY method

L memberwiseclone requires that the cloned object member be of the value type.

L a new class assigns the cloned class members one by one (physical activity)

L use. Net serialization and rice serialization (the performance is not as good as above)

Advantages and disadvantages

Because the prototype mode is similar to the abstract factory mode and the generator mode, it is available when selecting a mode in development without any complexity or other reference members in the object member to be cloned, if the prototype mode is used with caution when an object member contains a cyclic call member or a nested class member, the other two modes can be used to reduce the development difficulty.

Conclusion: Prototype Mode

Intention:

Specify the types of objects to be created based on the prototype strength, and create new objects based on the prototype.

Motivation:

In software systems, the creation of "some objects with complex structures" is often faced. due to changes in requirements, these objects are often faced with drastic changes, but they have relatively stable consistent interfaces.

Practicality:

L avoid creating too many factory classes

L dynamic creation type.

Applicability:

L when the class to be instantiated is specified at the runtime. (Such as dynamic loading)

L to avoid creating a factory level parallel to the product level

L when an instance of a class can only have one of several combinations of different States. Creating prototype numbers and cloning them may be more convenient than manually instantiating them with the appropriate state each time.

Structure:

 

Participants:

L prototype (icloneable)

L concreteprototype (danhuangpai)

L client (User)

Effect:

1. add and delete products at runtime

2. Change the value to specify a new object.

3. Change the structure to specify a new object

4. Reduce subclass Construction

5. dynamically configure applications with classes (some runtime environments allow you to dynamically load classes to applications)

Code:

The project code describes the clone method, the original method, and the memberwiseclone method in section 2.

 

Http://download.csdn.net/source/299970

 

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.