Java design pattern-prototype Pattern

Source: Internet
Author: User

Java design pattern-prototype Pattern
Overview

The prototype is used to create unnecessary objects. After the Cloneable interface is provided in Java JDK, the prototype becomes abnormal and simple. Although the introduction and use of Cloneable has become easier, there are still some things to be noted and explained. The text will explain in detail these considerations.

Detailed description of Copy Technology 1. Object assignment

If there is a Student object s1, when we assign a value to s2 using s2 = s1, the reference of the s1 object is actually copied to s2. The process is as follows:

Figure-1 memory Analysis of object assignment

Therefore, For s1 and s2, the memory should be in the same region. The memory addresses occupied by these two "objects" are as follows:

 

[email protected][email protected]
2. Shallow copy

What is a shallow copy? The result of the shallow copy is that the object's memory address has changed (the object's reference has changed), but the object's memory in the object has not changed.

Figure-2 Analysis of object copy memory

Do you have any questions about the above analysis chart? Why is the name changed, but friends does not?

Here, we do not need to mention other data types (int, double, char, short, long, float, byte). We know that objects of the String type are immutable objects, when we assign values to an immutable object, it opens up another object. Because this feature may affect our correct judgment on Cloneable, we need to use a variable object for experiment. I chose: List.

If the initial state is: s1 has two friends, XiaoHong and Xiaomei. When we use the variability of List to add a new friend to s2's friend List, in theory, the friend of s2 is modified, but this is not the case. The following is the experiment result:

Initial List object: s1.friends: [Xiao Hong, Xiao Mei] s2.friends: [Xiao Hong, Xiao Mei] The List object is not changed: s1.friends: [Xiao Hong, Xiao Mei, Xiao Gang] s2.friends: [Xiao Hong, Xiao Mei, xiaogang] assign a new value to the List object: s1.friends: [Xiaohong, Xiaomei, Xiaogang] s2.friends: null
From the experiment, we can see that if we perform internal variable operations on the object, the Copied object and the Copied object will be modified. From this point, we can conclude that Cloneable copying is a shortest copy. Do you have to ask, how can we achieve deep object copy? Next, let's look at it.

 

3. Deep copy

As mentioned above, we have some difficulties in operations on variable objects during the copy process. Deep copy can solve this problem. The implementation of deep copy depends on the persistence operation of objects.

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.