23 design patterns (3) Java prototype pattern
23 design patterns Part 3: Java prototype pattern
definition:
Create a new object instance by copying an existing object instance.
realization:
Implement clonable interface:
The clonable interface is used to notify the virtual machine at run time that the clone method can be safely used on the class that implements this interface. In the Java virtual machine, only classes that implement this interface can be copied. Otherwise, clonenotsupportedexception will be thrown at runtime.
Override the clone method in the object class:
In Java, the parent class of all classes is the object class. There is a clone method in the object class, which returns a copy of the object. However, its scope is of protected type, and general classes cannot call it. Therefore, the prototype class needs to modify the scope of the clone method to public type.
Example:
For example, for sending an invitation by email, most of the contents of the email are the same: the reason of the invitation, the place of the invitation, the gathering time, etc., but the name of the invitee and the email address are different.
Define the mail class:
Test method:
advantage:
1. Using the prototype model to create an object is more efficient than directly new an object, because it directly operates the binary stream in memory, especially when copying large objects. 2. It hides the complexity of making new instances, making creating objects as simple as copying and pasting when editing documents.
Disadvantages:
1. Because the construction method of the class will not be called when copying objects using the prototype pattern, the prototype pattern cannot be combined with the singleton pattern. Because the prototype class needs to modify the scope of the clone method to public type, the conditions of the singleton pattern cannot be met. 2. You cannot have a final object when using prototype mode. 3. The clone method of object class will only copy the basic data types in the object. For arrays and reference objects, you can only copy them separately. This involves the concepts of deep copy and shallow copy.
Deep and shallow copies:
Shallow copy:
After copying an object, the variables of the basic data type will be recreated, and the reference type still points to the original object (this is not safe).
Deep copy:
After copying an object, both basic data types and reference types are recreated.
So how to implement deep copy?
Continuing with the above example, an ArrayList attribute is added.
In this case, single mail = (mail) super clone(); The address area pointed to by ARS cannot be changed and must be copied separately:
Applicable scenarios:
1. Copy the structure and data of the object. 2. It is hoped that modifications to the target object will not affect the existing prototype object. 3. Creating an object is expensive.
Transferred from: Java confidant
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.