Detailed explanation of prototype mode

Detailed explanation of prototype mode

Question raised

  • In java, we know that objects created through the new keyword are very cumbersome (class loading judgment, memory allocation, initialization, etc.). When we need a large number of objects, the prototype pattern is the way we can consider the implementation.

Definition of prototype pattern

  • We have all used the copy and paste function. You can copy a file from one place to another. After copying, the file is no different from the previous file. This is the idea of prototype mode.

  • Using an instance that has been created as a prototype, create a new object that is the same or similar to the prototype by copying the prototype object. The prototype instance specifies the kind of object to create.

  • Prototype mode, also known as clone mode. An object as like as two peas, as like as two peas, are cloned from a prototype. And it has no effect on the prototype object.

Two cloning methods of prototype pattern

Shallow replication

  • If the member variable of the prototype object is a value type, a copy will be copied to the clone object;

  • If the member variable of the prototype object is a reference type, copy the address of the reference object to the clone object, that is, the member variables of the prototype object and the clone object point to the same memory address.

 

Deep replication

  • Whether the member variable of the prototype object is a value type or a reference type, it will be copied to the cloned object. Deep cloning will also copy all reference objects of the prototype object to the cloned object.
  • In deep cron, in addition to the object itself being copied, all member variables contained in the object will also be copied.
  • All variables of the copied object contain the same value as the original object, except those variables that reference other objects.
  • Variables that refer to other objects will point to the new objects that have been copied, rather than the original referenced objects.
  • In other words, deep copy copies all the objects referenced by the object to be copied.

 

Three common implementation methods of prototype pattern

  • Using constructor method

  • Using clonable interface method

  • Serializable interface method

Using constructor method

Using clonable interface method

Clone - shallow copy

Clone - deep copy

public class Work implements Cloneable {
    String name;
    int age;
    PubInfo pubInfo;
    public Work(String name, int age, PubInfo pubInfo) {
        this.name = name;
        this.age = age;
        this.pubInfo = pubInfo;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
​
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public PubInfo getPubInfo() {
        return pubInfo;
    }
    public void setPubInfo(PubInfo pubInfo) {
        this.pubInfo = pubInfo;
    }
    public Work(Work w) {
        name = w.getName();
        age = w.getAge();
        pubInfo = w.getPubInfo();
    }
​
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Work w =(Work)super.clone();
        w.setPubInfo((PubInfo)pubInfo.clone());
        return w;
    }
}
public class PubInfo implements Cloneable{
    String uni;
    String city;
    public PubInfo(String uni, String city) {
        this.uni = uni;
        this.city = city;
    }
​
    public String getUni() {
        return uni;
    }
​
    public void setUni(String uni) {
        this.uni = uni;
    }
​
    public String getCity() {
        return city;
    }
​
    public void setCity(String city) {
        this.city = city;
    }
    public PubInfo(PubInfo pubInfo){
        uni = pubInfo.getUni();
        city = pubInfo.getCity();
    }
​
    @Override
    protected Object clone() throws CloneNotSupportedException {
        PubInfo p =(PubInfo)super.clone();
        return p;
    }
}
public class Demo01 {
    public static void main(String[] args) throws Exception {
        PubInfo pubInfo = new PubInfo("aa", "bb");
        Work w = new Work("Zhang Shi", 18, pubInfo);
        Work w2 = (Work)w.clone();
        System.out.println("W="+w+"\nw2="+w2);
        System.out.println("w.name="+w.getName()+"\nw2.name="+w2.getName());
        System.out.println(w.name.hashCode());
        System.out.println(w2.name.hashCode());
        System.out.println("w.pubInfo="+w.getPubInfo()+"\nw2.pubInfo="+w2.getPubInfo());
    }
}

 

Serializable interface method

Tags: Java

Posted by bosco500 on Thu, 14 Apr 2022 23:53:39 +0930