Three-minute understanding of "prototype mode"-Easy to master in design mode

Source: Internet
Author: User

The official definition of the prototype model:

Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.


Plain English

Now there is a class that produces a large number of objects, and most of the property values in these objects are the same; if we are going to get such an object each time we pass new and set each property value, then it will be too much trouble. In this case, it is very convenient to use the prototype mode:

We let this class implement the ICloneable interface, and implement the Clone () function of the interface, make a shallow copy/deep copy of the current object in the Clone function, in short, clone a current object, so that we do not have to new end the object after the Set property.


Class diagram of the prototype pattern:


If a class is to enable the caller to clone, it is necessary to implement the ICloneable interface when defining the class, and implement the clone function to implement a shallow or deep copy of the current object in the function.

After the client has taken this class, if the object needs to be cloned, call the clone function of the class.


Deep copy and shallow copy:

Shallow copy: Copies the value of the base data type in the object to the new object, and if the object has a property of a reference type, the reference is copied to the new object, and the pointer to the reference type in the old and new object points to the same object.


As shown: The phone in the new object is the base data type, its value is copied directly from the old object, and the person in the new object is the reference type, and the perosn in the old object point to the same person object.

Deep copy: Deep copy and shallow copy are identical for the base data type, except that the variable of the reference type in the new and old object is pointed to the same object after the shallow copy, and the deep copy when the object of the reference type is encountered, then new object, the reference type variable in the new object points to the newly created object, as shown in:



To implement a prototype pattern using a deep copy:


As shown, the prototype class has a member variable of the reference type person, and the person class has a member variable of the reference type work, to implement a deep copy, in order to make the prototype class, person class, work class of the clone function to achieve deep copy function, The clone function that implements the deep copy is as follows:

Public Object Clone () {

Make a shallow copy of the current object

prototypes p =this. Clone ();

Makes a deep copy of a variable of the reference type in the current object. Since the person class also implements the ICloneable interface, and the clone function does the same thing, calling the work class's clone function, because the work class is all basic data types, the work class does not need to implement the ICloneable interface, Cloning a work class requires only a shallow copy of the Clone () function provided by object.

P. Setperson (this. person. Clone ());

returnp;

}


The Clone function in Java:

There is a clone () function in the object class in Java, and this function is a shallow copy of the object. When we need to make a deep copy of an object, we have to make the class implement the Cloneable interface and rewrite the Clone () function.

Deep and shallow copies in Java please move to: http://blog.csdn.net/shootyou/article/details/3945221

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Three-minute understanding of "prototype mode"-Easy to master in design 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.