Design mode six, prototype mode

Prototype mode : Use prototype instances to specify the types of objects to be created, and create new objects by copying these prototypes.

The prototype mode is to create another customizable object from one object without knowing any details of creation.

Shallow copy, deep copy

Shallow copy, copy the value of the type field of the real, the real domain reference type reference object does not replicate replicate only reference, i.e., cloned object band the original object areferencing real domain of the same object type c, the b的方法changes b中的cwill affect a中的c.

//简历类,实现Cloneable接口
public class Resume implements Cloneable {
    
    
    private String name;
    private String sex;
    private String age;
    private String time;
    private String company;

    public Resume(String name) {
    
    
        this.name = name;
    }

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

    public void SetWork(String time, String company) {
    
    
        this.time = time;
        this.company = company;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        Object object = super.clone();
        return object;
    }

    @Override
    public String toString() {
    
    
        return "Resume{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age='" + age + '\'' +
                ", time='" + time + '\'' +
                ", company='" + company + '\'' +
                '}';
    }

    public static void main(String[] args) throws CloneNotSupportedException {
    
    
        Resume resume1 = new Resume("RagdoLL");
        resume1.SetInfo("man", "21");
        resume1.SetWork("2022", "Ubisoft");

        Resume resume2 = (Resume) resume1.clone();

        System.out.println(resume1.toString());
        System.out.println(resume2.toString());
    }
}

Output:

Resume{
    
    name='RagdoLL', sex='man', age='21', time='2022', company='Ubisoft'}
Resume{
    
    name='RagdoLL', sex='man', age='21', time='2022', company='Ubisoft'}

If the object is changed to Workhold timeand companyreal domain, shallow copy can not complete copy of the referenced object, copy only the references

//简历类,实现Cloneable接口
public class Resume implements Cloneable {
    
    
    private String name;
    private String sex;
    private String age;

    private Work work;

    public Resume(String name) {
    
    
        this.name = name;
        work = new Work();
    }

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

    public void SetWork(String time, String company) {
    
    
        this.work.setTime(time);
        this.work.setCompany(company);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        Object object = super.clone();
        return object;
    }

    @Override
    public String toString() {
    
    
        return "Resume{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age='" + age + '\'' +
                ", time='" + work.getTime() + '\'' +
                ", company='" + work.getCompany() + '\'' +
                '}';
    }

    public static void main(String[] args) throws CloneNotSupportedException {
    
    
        Resume resume1 = new Resume("RagdoLL");
        resume1.SetInfo("man", "21");
        resume1.SetWork("2022", "Ubisoft");

        Resume resume2 = (Resume) resume1.clone();
        resume2.SetWork("2021","Action");

        System.out.println(resume1.toString());
        System.out.println(resume2.toString()); //改变resume2的Work,resume1的Work对象的值被覆盖
    }
}

Deep copy, Workalso copy the referenced object .

//简历类,实现Cloneable接口
public class Resume implements Cloneable {
    
    
    private String name;
    private String sex;
    private String age;

    private Work work;

    public Resume(String name) {
    
    
        this.name = name;
        work = new Work();
    }

    //克隆引用对象的接口
    public void setWork(Work work) {
    
    
        this.work = work;
    }

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

    public void SetWork(String time, String company) {
    
    
        this.work.setTime(time);
        this.work.setCompany(company);
    }

    @Override
    protected Resume clone() throws CloneNotSupportedException {
    
    
        //这一步还是浅克隆
        Resume resume = (Resume) super.clone();
        //完成深克隆
        resume.setWork((Work) work.clone());
        return resume;
    }

    @Override
    public String toString() {
    
    
        return "Resume{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age='" + age + '\'' +
                ", time='" + work.getTime() + '\'' +
                ", company='" + work.getCompany() + '\'' +
                '}';
    }

    public static void main(String[] args) throws CloneNotSupportedException {
    
    
        Resume resume1 = new Resume("RagdoLL");
        resume1.SetInfo("man", "21");
        resume1.SetWork("2022", "Ubisoft");

        Resume resume2 = resume1.clone();
        resume2.SetWork("2021", "Action");

        System.out.println(resume1.toString());
        System.out.println(resume2.toString());
    }
}

Output:

Resume{
    
    name='RagdoLL', sex='man', age='21', time='2022', company='Ubisoft'}
Resume{
    
    name='RagdoLL', sex='man', age='21', time='2021', company='Action'}

Guess you like

Origin blog.csdn.net/weixin_45401129/article/details/114629250