Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Java prototype pattern

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of the Java prototype pattern, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

1. Think about the problem

Now there is a sheep tom, the name is: tom, the age is: 1, the color is: White, please write a program to create 10 sheep with exactly the same attributes as tom sheep.

According to the traditional way of thinking, we may write in the following way.

Then the pros and cons of this way of writing come out naturally:

The advantage is that it is easy to understand and easy to operate.

The disadvantage is that when you create a new object, you always need to retrieve the properties of the original object, which is less efficient if the object you create is more complex. It is not flexible enough that you always need to reinitialize the object rather than dynamically obtain the state of the object at run time.

Improved idea analysis: the Object class in Java is the root class of all classes, and the Object class provides a clone () method, which can copy a Java object, but the Java class that needs to implement clone must implement an interface Cloneable, which indicates that the class can copy and has the ability to copy = > prototype pattern.

two。 What is the prototype pattern?

Prototype pattern (Prototype pattern) means to specify the type of object to be created with prototype instances, and to create new objects by copying these prototypes.

A prototype pattern is a creative design pattern that allows one object to create another customizable object without knowing the details of how to create it.

How it works: by passing a prototype object to the object to be created, the object to be created is created by requesting the prototype object to copy itself, that is, object .clone ().

The class diagrams related to the prototype pattern are as follows:

3. Cloned sheep Dolly case code (shallow copy)

Since I am a newly built ordinary Java project, not a maven project, I cannot add lombok dependencies. So the constructor and setter/getter method here seem to be quite long.

Package com.szh.prototype.shallowclone; public class Sheep implements Cloneable {private String name; private Integer age; private String color; private Sheep friend; public Sheep (String name, Integer age, String color) {this.name = name; this.age = age; this.color = color;} public Sheep (String name, Integer age, String color, Sheep friend) {this.name = name; this.age = age This.color = color; this.friend = friend;} public String getName () {return name;} public void setName (String name) {this.name = name;} public Integer getAge () {return age;} public void setAge (Integer age) {this.age = age;} public String getColor () {return color } public void setColor (String color) {this.color = color;} public Sheep getFriend () {return friend;} public void setFriend (Sheep friend) {this.friend = friend } @ Override public String toString () {return "Sheep {" + "name='" + name +'\'+ ", age=" + age + ", color='" + color +'\'+ ", friend=" + friend +'}' } / / Clone the instance and use the default clone method to complete @ Override protected Object clone () {Sheep sheep = null; try {sheep = (Sheep) super.clone ();} catch (CloneNotSupportedException e) {System.out.println (e.getMessage ());} return sheep;}} package com.szh.prototype.shallowclone Public class MainTest {public static void main (String [] args) {Sheep sheep = new Sheep ("Dolly", 5, "Black and White", new Sheep ("Pleasant Goat", 1, "White"); Sheep sheep2 = (Sheep) sheep.clone (); Sheep sheep3 = (Sheep) sheep.clone (); Sheep sheep4 = (Sheep) sheep.clone (); Sheep sheep5 = (Sheep) sheep.clone () System.out.println (sheep + "sheep.friend.hashCode =" + sheep.getFriend (). HashCode ()); System.out.println (sheep2 + "sheep2.friend.hashCode =" + sheep2.getFriend (). HashCode ()); System.out.println (sheep3 + "sheep3.friend.hashCode =" + sheep3.getFriend (). HashCode ()); System.out.println (sheep4 + "sheep4.friend.hashCode =" + sheep4.getFriend (). HashCode ()) System.out.println (sheep5 + "sheep5.friend.hashCode =" + sheep5.getFriend (). HashCode ();}}

As you can see from the above run results, the first three member attributes in the Sheep class can be successfully copied, but the last friend, which represents the friend of the sheep (also the Sheep type, that is, the reference type). When we copy it, it should all be different new objects, but the hashCode of the friend attribute in them is the same! Here we are going to talk about deep copy and shallow copy.

Shallow copy:

For member variables whose data type is the basic data type, the shallow copy passes the value directly, that is, a copy of the property value is copied to the new object.

For a member variable whose data type is a reference data type, for example, a member variable is an array, a class object, etc., then a shallow copy will pass the reference, that is, simply copy the reference value of the member variable (memory address > copy to the new object). Because in fact, the member variable of both objects points to the same instance. In this case, modifying the member variable in one object affects the value of the member variable in another object.

In front of us, we cloned the sheep is a shallow copy. Shallow copy is implemented using the default clone () method: sheep= (Sheep) super.clone ()

Deep copy:

Copies the member variable values of all basic data types of the object.

Request storage space for all member variables of the reference data type, and copy the objects referenced by each reference data type member variable until all objects that are reachable by that object. That is, a deep copy of an object requires a copy of the entire object, including the reference type of the object.

Deep copy implementation 1: rewrite the clone method to achieve deep copy.

Deep copy implementation 2: deep copy through object serialization (recommended).

4. Deep copy code case package com.szh.prototype.deepclone; import java.io.Serializable; public class DeepCloneableTarget implements Serializable, Cloneable {private static final long serialVersionUID = 1L; private String cloneName; private String cloneClass; public DeepCloneableTarget (String cloneName, String cloneClass) {this.cloneName = cloneName; this.cloneClass = cloneClass } @ Override public String toString () {return "DeepCloneableTarget {" + "cloneName='" + cloneName +'\'+ ", cloneClass='" + cloneClass +'\'+'}' } / / because the properties of this class are all String, we can use the default clone completion here to @ Override protected Object clone () throws CloneNotSupportedException {return super.clone ();}} package com.szh.prototype.deepclone; import java.io.*; public class DeepPrototype implements Serializable, Cloneable {public String name; / / String attribute public DeepCloneableTarget deepCloneableTarget;// reference type public DeepPrototype () {super () } @ Override public String toString () {return "DeepPrototype {" + "name='" + name +'\'+ ", deepCloneableTarget=" + deepCloneableTarget +'}';} / / Deep copy-Mode 1 uses the clone method @ Override protected Object clone () throws CloneNotSupportedException {Object deep = null; deep = super.clone () DeepPrototype deepPrototype = (DeepPrototype) deep; deepPrototype.deepCloneableTarget = (DeepCloneableTarget) deepCloneableTarget.clone (); return deepPrototype;} / / Deep copy-Mode 2 is implemented through object serialization (recommended) public Object deepClone () {/ / create stream object ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; ByteArrayInputStream bis = null; ObjectInputStream ois = null Try {/ / serialize bos = new ByteArrayOutputStream (); oos = new ObjectOutputStream (bos); oos.writeObject (this); / / deserialize bis = new ByteArrayInputStream (bos.toByteArray ()); ois = new ObjectInputStream (bis); DeepPrototype copyObj = (DeepPrototype) ois.readObject (); return copyObj } catch (Exception e) {e.printStackTrace (); return null;} finally {/ / close the stream try {ois.close (); bis.close (); oos.close (); bos.close () } catch (Exception e2) {System.out.println (e2.getMessage ());} package com.szh.prototype.deepclone; public class MainTest {public static void main (String [] args) throws Exception {DeepPrototype prototype = new DeepPrototype (); prototype.name = "Zhang Qiling"; prototype.deepCloneableTarget = new DeepCloneableTarget ("little brother", "stuffy bottle") / / Mode 1 completes deep copy DeepPrototype prototype2 = (DeepPrototype) prototype.clone (); System.out.println ("Mode 1 completes deep copy"); System.out.println ("prototype.name =" + prototype.name + ", prototype.deepCloneableTarget =" + prototype.deepCloneableTarget); System.out.println ("prototype.deepCloneableTarget.hashCode =" + prototype.deepCloneableTarget.hashCode ()) System.out.println ("- -"); System.out.println ("prototype2.name =" + prototype2.name + ", prototype2.deepCloneableTarget =" + prototype2.deepCloneableTarget); System.out.println ("prototype2.deepCloneableTarget.hashCode =" + prototype2.deepCloneableTarget.hashCode ()) System.out.println ("="); / / Mode 2 completes deep copy DeepPrototype prototype3 = (DeepPrototype) prototype.deepClone (); System.out.println ("Mode 2 completes deep copy"); System.out.println ("prototype.name =" + prototype.name + ", prototype.deepCloneableTarget =" + prototype.deepCloneableTarget); System.out.println ("prototype.deepCloneableTarget.hashCode =" + prototype.deepCloneableTarget.hashCode ()) System.out.println ("- -"); System.out.println ("prototype3.name =" + prototype3.name + ", prototype3.deepCloneableTarget =" + prototype3.deepCloneableTarget); System.out.println ("prototype3.deepCloneableTarget.hashCode =" + prototype3.deepCloneableTarget.hashCode ()) }}

5. Summary of prototype pattern

When creating a new object is complex, you can use the prototype pattern to simplify the object creation process and improve efficiency.

Instead of reinitializing the object, it dynamically obtains the state of the object at run time.

If the original object changes (adding or decreasing attributes), other cloned objects will change accordingly, and there is no need to modify the code.

More complex code may be required to implement deep cloning.

Disadvantages: each class needs to be equipped with a clone method, which is not very difficult for a new class, but when you modify an existing class, you need to modify its source code, which violates the ocp principle.

Thank you for reading this article carefully. I hope the article "sample Analysis of Java prototype pattern" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report