Java design pattern-prototype mode

Source: Internet
Author: User
Tags shallow copy

Overview

The prototype pattern is designed to solve some unnecessary object creation processes. When the Cloneable interface is available in the Java JDK, the prototype schema becomes unusually simple. Although the introduction of cloneable has become simpler, there are some things that need to be noted and noticed. The text will explain these precautions in detail.


Copyright Notice

Copyright belongs to the author.
Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Coding-naga
Published: March 3, 2016
Links: http://blog.csdn.net/lemon_tree12138/article/details/50787390
Source: CSDN
More information: Classification >> Design Patterns


Copy Technology Detailed1. Assigning Values to Objects

If there is now a Student object S1, when we use S2=S1 to assign a value to S2, we are actually copying the reference of the S1 object to S2. The process is as follows:


Figure-1 Object Assignment Memory analysis diagram

Therefore, for S1 and S2, the memory they refer to should be the same area. The following memory addresses are used for this two "objects":

[Email protected] [Email protected]


2. Shallow copy

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


Figure-2 object Copy memory analysis diagram

Do you see some doubts about the above analysis chart? Why has the name changed, and friends has not changed?

Let alone the other data types (int, double, char, short, long, float, byte), we know that the object of type string is an immutable object, and when we assign a value to an immutable object, it opens up another new object. Because this feature may affect our correct judgment on cloneable, it is necessary to experiment with a mutable object. I chose to: List.

If the initial state is: S1 has two friends, Xiao Red and Xiao Mei. When we use the variability of list to add a new friend to S2 's friends list, it is theoretically only that S2 's friends have been modified, but this is not the case, and the following are experimental results:

Initial List object: s1.friends: [Xiao Hong, Xiao Mei]s2.friends: [Little Red, Xiao Mei] does not change the list object: s1.friends: [Little Red, Xiao Mei, Xiao Gang]s2.friends: [Little Red, Xiao Mei, Xiao Gang] To re-assign a value to the list object: s1.friends: [Little Red, Xiao Mei, Xiao Gang]s2.friends:null
From the experiment we can see that if we do variable objects inside the object, the copied objects and the copied objects will be modified. From this point alone, we can conclude that the cloneable copy belongs to a shallow copy. Do you have to ask, how can we achieve the deep copy of the object? Please look down.

3. Deep copy

The shallow copy of the object is mentioned above, and we have some tricky operations on mutable objects in the copy. Deep copy can solve this problem. The implementation of a deep copy relies on persistent operations with the object. More about object persistence can be found in my other blog: "Web crawler: Based on the persistence of objects to achieve the crawler site rapid reduction."

Object persistence is about writing an object to a file (either to the network or to a process), and when we need to copy an object, we first solidify the object to a file and then read the object from the file. Such a process to complete the deep copy of the object, in the blog " web crawler: Object-based persistence to implement the crawler site rapid Restoration ," I also explained in detail, here is not to repeat.


prototype Pattern Implementation

Finally, we can say the theme of this article (prototype mode).

The prototype pattern is an object-based copy, which can be a shallow copy or a deep copy operation. In other words, when we need to batch build an object, we can create a prototype of the object in advance, and then build the object in bulk by copying the object. The implementation class diagram for the prototype pattern is as follows:


Figure-3 Prototype pattern class diagram

Ref

    • "23 Java Design Patterns"
    • "Writing High-quality code: 151 recommendations for improving Java programs"


GitHub Source Download

Https://github.com/William-Hai/DesignPattern-Prototype

Java design pattern-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.