The Prototype Model of Dahua Design Mode

Posted by trp on Tue, 14 Jul 2020 18:17:00 +0200

1. Definition

Use prototype instances to develop the types of objects created, and copy them to create new objects that can be used to create duplicate objects while maintaining performance.Implement clone operation, inherit Cloneable in JAVA, override clone().

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

Shallow and deep copies

If the field is of value type, the field is copied bit by bit.
If the field is of reference type, the referenced object is copied but not the referenced object, and the original object and its copy refer to the same object.

Shallow copy: All variables of the copied object contain the same value as the original object, and references to other objects still point to the original object.That is, the shallow copy is only responsible for the current object instance and does not copy the referenced object.
Deep copy: A new object that copies the variables of the referenced object instead of the original referenced object.

2. Implementation

2.1 Shallow Copy

public class WordExperience {
    String workDate;
     String company;    
}
public class Resume implements Cloneable  {
    private String name;//Value type
    private String sex;//Value type
    private String age;//Value type
    private WordExperience work;//reference type

     public Resume(String name) {
         this.name=name;
         work=new WordExperience();
     }
     @Override  
     public Resume clone() throws CloneNotSupportedException {  
         return (Resume) super.clone();     
     }

    public void Display() {
        System.out.print("name:"+name+",sex:"+sex+",age:"+age);
        System.out.println(",work.workDate:"+work.workDate+",work.company:"+work.company);
    }

    public void SetPersonalInfo(String sex, String age) {
        this.sex=sex;
        this.age=age;

    }

    public void SetWordExperience(String timeArea, String company) {
        work.workDate=timeArea;
        work.company=company;
    }

}
    public static void main(String[] args) throws CloneNotSupportedException {
        Resume a=new Resume("ting");
        a.SetPersonalInfo("man", "29");
        a.SetWordExperience("1998-2000", "A");

        Resume b=(Resume)a.clone();
        b.SetPersonalInfo("woman", "30");
        b.SetWordExperience("2000-2016", "B");

        a.Display();
        b.Display();    
    }

Output:
name:ting,sex:man, age:29,work.workDate:2000-2016,work.company:B
name:ting,sex:woman,age:30,work.workDate:2000-2016,work.company:B
Work experience shows the last set value, while sex and age can be displayed separately.

2.2 deep copy

public class WordExperience implements Cloneable{
    public String workDate;
    public String company;
    @Override//"Working Experience Implements Cloning Method"
    public WordExperience clone() throws CloneNotSupportedException {
        return (WordExperience)super.clone();
    }
}
public class Resume implements Cloneable  {
    private String name;
    private String sex;
    private String age;
    private WordExperience work;

     public Resume(String name) {
         this.name=name;
         work=new WordExperience();
     }
     public  Resume(WordExperience wrok) throws CloneNotSupportedException {
         this.work=wrok.clone();
     }
     @Override  
     public Resume clone() throws CloneNotSupportedException {  
         Resume resume=new Resume(this.work);
         resume.name=name;
         resume.sex=sex;
         resume.age=age;
           return resume;  
     }

    public void Display() {
        System.out.print("name:"+name+",sex:"+sex+",age:"+age);
        System.out.println(",work.workDate:"+work.workDate+",work.company:"+work.company);
    }

    public void SetPersonalInfo(String sex, String age) {
        this.sex=sex;
        this.age=age;

    }

    public void SetWordExperience(String workDate, String company) {
        work.workDate=workDate;
        work.company=company;
    }

}
    public static void main(String[] args) throws CloneNotSupportedException {
        Resume a=new Resume("daniao");
        a.SetPersonalInfo("man", "29");
        a.SetWordExperience("1998-2000", "ting");

        Resume b=(Resume)a.clone();
        b.SetWordExperience("2000-2016", "ben");

        a.Display();
        b.Display();


    }
}

Output:
name:daniao,sex:man,age:29,work.workDate:1998-2000,work.company:ting
name:daniao,sex:man,age:29,work.workDate:2000-2016,work.company:ben

Topics: Java