Java Design Pattern cainiao series (16) prototype pattern modeling and implementation
Prototype: This mode uses an object as a Prototype to copy, clone, and generate a new object similar to the original object. Here, there are two types of replication: shallow replication and deep replication.
Light Replication: After copying an object, all the variables of the basic data type are re-created, and the reference type points to the original object.
Deep Replication: After an object is copied, both the basic data type and the reference type are re-created. In short, deep replication is completely complete, but not complete.
The following is an example:
1. Shallow Copy 1. uml modeling:
2. Code Implementation
/*** Prototype: Copy and clone an object as a prototype to generate a new object similar to the original object ]. ** There are two types of replication: shallow copy and deep copy ** example (1) shallow copy: After copying an object, all the variables of the basic data type will be recreated, ** the reference type still points to the original object and is not re-created ]. */Class Prototype implements Cloneable {private int age; private int [] array = new int [] {1, 2, 3}; public Prototype () {} public Prototype (int age) {this. age = age;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public int [] getArray () {return array;} public void setArray (int [] array) {this. array = array;}/*** because the Cloneable interface is an empty interface ** the focus here is super. clone (), super. clone () The Object clone () method is called. In the Object class, clone () is native. ** this involves JNI. We will discuss JNI and NDK later, here, you just need to remember that the core of shap replication is super. clone (). */Public Object cloneObject () throws CloneNotSupportedException {Prototype prototype = (Prototype) super. clone (); return prototype;}/*** client Test class ** @ author Leo */public class Test {public static void main (String [] args) throws CloneNotSupportedException {Prototype prototype = new Prototype (20); Prototype cloneProto = (Prototype) prototype. cloneObject ();/*** you can see through printing: prototype And cloneProto are two variables of the same type. It points to two different memory addresses. ** this indicates that the clone is successful */System. out. println (prototype = + prototype); System. out. println (cloneProto = + cloneProto);/*** to completely copy an object, then its reference type variable array must point to a different memory address ** and here the reference type variable array points to the original object. The printed memory address is the same. ** This indicates that the object is not completely copied */System. out. println (prototype. getArray () = + prototype. getArray (); System. out. println (cloneProto. getArray () = + cloneProto. getArray ();/*** in this example, we can see that the object is not completely copied in shortest copy */}}
Ii. Deep Replication
1. uml modeling:
2. Code Implementation
/*** Example (2) Deep copy: After an object is copied, it is [re-created] regardless of the basic data type or reference type. ** In short, deep replication is completely complete, but not complete. ** Because reading and writing objects is involved here, Object serialization is used here-the Serializable interface */class Prototype implements Cloneable, Serializable {private static final long serialVersionUID = 1L; private int age; private int [] array = new int [] {1, 2, 3}; public Prototype () {} public Prototype (int age) {this. age = age;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public int [] getArray () {return array;} public Void setArray (int [] array) {this. array = array;}/* Deep copy */public Object deepClone () throws IOException, ClassNotFoundException {/* write the binary stream of the current Object */ByteArrayOutputStream bos = new ByteArrayOutputStream (); objectOutputStream oos = new ObjectOutputStream (bos); oos. writeObject (this);/* read the new object generated by binary abortion */ByteArrayInputStream bis = new ByteArrayInputStream (bos. toByteArray (); ObjectInputStream ois = new ObjectI NputStream (bis); return ois. readObject () ;}/ *** client Test class ** @ author Leo */public class Test {public static void main (String [] args) throws IOException, classNotFoundException {Prototype prototype = new Prototype (20); Prototype cloneProto = (Prototype) prototype. deepClone ();/*** it can be seen through printing: prototype And cloneProto. The two variables of the same type point to two different memory addresses. ** this indicates the cloning is successful */System. out. println (prototype = + prototype); System. out. p Rintln (cloneProto = + cloneProto);/*** it can be seen through printing, the reference type variable array of the two objects points to different memory addresses. ** this indicates that the object has been completely copied */System. out. println (prototype. getArray () = + prototype. getArray (); System. out. println (cloneProto. getArray () = + cloneProto. getArray ();/*** of course, we can also print the content of the referenced variable. ** we can see that the content is unchanged (1 2 3 ), only the memory address pointed to by the referenced variable is changed. */Int [] proArray = prototype. getArray (); int [] cloneProtoArray = cloneProto. getArray (); for (int p: proArray) {System. out. print (p +);} System. out. println (); for (int p: cloneProtoArray) {System. out. print (p + );}}}
Iii. Summary
1. The core of shortest replication is super. clone (), which calls the Object clone () method. In the Object class, clone () is native.
2. To achieve deep replication, you need to write the current object in the form of a binary stream and then read it.