The design pattern of the vegetable--prototype mode

Source: Internet
Author: User


background

Sometimes you will find that the reference type is also very annoying, because at some point you may need two of the same reference object, one for backup, one to continue operation, but continue operation that does not affect my backup that, this time the trouble comes, two references if point to the same memory space object, Then modify one, and the other will certainly change.

1. Intention to use

Instead of re-initializing the object, you dynamically get the state of the object runtime.

2. Life Example

The process of setting up a copy is a prototype model, where you can make targeted changes to a specific area, and then copy it again with the modified data!

3. Java Example (framework, JDK, JEE)

Not clear where if anyone knows can add

4. Pattern class Diagram

5. Advantages of the Model

Prototype mode: Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes.

The prototype pattern is actually one more customizable object from one object and no need to know the details of any creation.

Cloning is generally the best way to initialize information without changing it, cloning both hides the details of the object creation, and there is a significant improvement in performance.

Instead of re-initializing the object, you dynamically get the state of the object runtime.

Prototype mode using cloning will certainly involve deep replication and shallow copy, Java implementation of the direct substitution clone method, but that is shallow copy, the true use of deep replication also need to implement the serializable interface, or the object type of data deep depth clone.

6. Comparison with similar models

The feeling prototype pattern is used very little, because he is a backup of the object data, when we define a class, we often give some of his member variables to initialize, do not know if such a new object is not the same as the prototype model is the same reason? Oh..........


The design pattern of the vegetable--prototype mode

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.