The contents are collected and sorted online for their own learning and taking notes
1. Definitions
the so-called prototype mode is nothing more than creating another customizable object from one object without knowing any creation details. The essence of the so-called prototype mode is the cloning technology in programming. Taking an object as a prototype and copying a new object, we just need to pay attention to the problems of deep replication and shallow replication
2. Shallow replication
in Java, the member variable of basic data type is completely copied to the new object during shallow copy. For the member variable of reference data type, it is directly copied and referenced, so that the two objects actually operate on the same object
/* Copy construction method to realize shallow copy */ public class CopyConstructor { public static void main(String[] args) { Age a=new Age(20); Person p1=new Person(a,"Shake your head Jesus"); Person p2=new Person(p1); System.out.println("p1 yes"+p1); System.out.println("p2 yes"+p2); //Modify the attribute values of p1 and observe whether the attribute values of p2 change with each other p1.setName("Little fool"); a.setAge(99); System.out.println("Modified p1 yes"+p1); System.out.println("Modified p2 yes"+p2); } } class Person{ //Two attribute values: Value Passing and reference passing private Age age; private String name; public Person(Age age,String name) { this.age=age; this.name=name; } //Copy construction method public Person(Person p) { this.name=p.name; this.age=p.age; } public void setName(String name) { this.name=name; } public String toString() { return this.name+" "+this.age; } } class Age{ private int age; public Age(int age) { this.age=age; } public void setAge(int age) { this.age=age; } public int getAge() { return this.age; } public String toString() { return getAge()+""; } } //p1 is Jesus shaking his head 20 //p2 is Jesus shaking his head 20 //The modified p1 is little fool 99 //The modified p2 is 99
result analysis: two representative attribute values are selected for the Person class: one is the reference passing type; The other is string type (it belongs to a constant). A shallow copy is made through the copy construction method, and each attribute value is successfully copied. Among them, p2 will not change when the attribute value of p1 value transfer part changes; and p2 will also change when the attribute value of reference transfer part changes. Note: if in the copy construction method, open up new memory space for reference data type variables one by one to create Creating new objects can also realize deep copy. For the general copy structure, it must be a shallow copy.
3. Deep replication
deep copy is a complete copy of the attributes of the reference type, rather than just copying the reference address There are generally two ways to complete deep replication: one is to copy the clone method and call the clone method of the reference object, and the other is to complete it by serialization
public class DeepCopy { public static void main(String[] args) { Age a=new Age(20); Student stu1=new Student("TestMan",a,175); //Make a shallow copy by calling the overridden clone method Student stu2=(Student)stu1.clone(); System.out.println(stu1.toString()); System.out.println(stu2.toString()); System.out.println(); //Try to modify the attributes of stu1 and observe whether the attributes of stu2 have changed stu1.setName("Big fool"); //Change the value of the member variable of this reference type a.setAge(99); //stu1.setaAge(new Age(99)); If you modify the age attribute value in this way, stu2 will not change with it. Because a new age class object is created instead of changing the instance value of the original object stu1.setLength(216); System.out.println(stu1.toString()); System.out.println(stu2.toString()); } } /* * Create age class */ class Age implements Cloneable{ //Member variable (attribute) of age class private int age; //Construction method public Age(int age) { this.age=age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return this.age+""; } //Override the clone method of Object public Object clone() { Object obj=null; try { obj=super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return obj; } } /* * Create student class */ class Student implements Cloneable{ //The member variable (attribute) of the student class, one of which is the object of the class private String name; private Age aage; private int length; //Construction method, in which one parameter is an object of another class public Student(String name,Age a,int length) { this.name=name; this.aage=a; this.length=length; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Age getaAge() { return this.aage; } public void setaAge(Age age) { this.aage=age; } public int getLength() { return this.length; } public void setLength(int length) { this.length=length; } public String toString() { return "The name is: "+this.getName()+",Age:"+this.getaAge().toString()+", The length is: "+this.getLength(); } //Override the clone method of the Object class public Object clone() { Object obj=null; //Call clone method of Object class -- shallow copy try { obj= super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } //Call the clone method of the Age class for deep copy //First convert obj into a student class instance Student stu=(Student)obj; //Here is the key to using the clone method to realize deep replication. Call the clone method of object properties to complete deep replication //The Age object attribute of the student class instance and call its clone method to copy stu.age=(Age)stu.getaAge().clone(); return obj; } }
clone method is a simple way to implement the prototype pattern provided by JDK, but we need to pay attention to the difference between shallow replication and deep replication.