大话设计模式----原型模式

夙愿已清 提交于 2020-04-04 00:34:29

一、原型模式:用原型示例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

浅复制:

代码

@Data
public class WorkExperience {

    private String workDate;

    private String company;
}

@Data
public class Resume implements Cloneable {

    private String name;
    private String sex;
    private String age;

    private WorkExperience workExperience;

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

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

    public void setWorkExperience(String workDate, String company){
        workExperience.setCompany(company);
        workExperience.setWorkDate(workDate);
    }

    public void display(){
        System.out.println("name = " + name + ",sex = " + sex + ",age = " + age);
        System.out.println("工作经历:" + workExperience.getWorkDate() + "-" + workExperience.getCompany());
    }

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

public class Test {

    public static void main(String[] args) throws CloneNotSupportedException {
        Resume resumeA = new Resume("杨过");
        resumeA.setPersonalInfo("M","29");
        resumeA.setWorkExperience("2020-04-03","阿里巴巴");

        Resume resumeB = (Resume) resumeA.clone();
        resumeB.setWorkExperience("2020-01-01","腾讯");

        Resume resumeC = (Resume) resumeA.clone();
        resumeC.setPersonalInfo("F","24");

        resumeA.display();
        resumeB.display();
        resumeC.display();

        /**
         * 运行结果(三个引用都是最后一次设置的值,因为三个引用都指向了一个对象)
         *
         * name = 杨过,sex = M,age = 29
         * 工作经历:2020-01-01-腾讯
         * name = 杨过,sex = M,age = 29
         * 工作经历:2020-01-01-腾讯
         * name = 杨过,sex = F,age = 24
         * 工作经历:2020-01-01-腾讯
         */
    }
}

深复制:

@Data
public class WorkExperience implements Cloneable{

    private String workDate;

    private String company;

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

@Data
public class Resume implements Cloneable {

    private String name;
    private String sex;
    private String age;

    private WorkExperience workExperience;

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

    public Resume(WorkExperience workExperience) throws CloneNotSupportedException {
        this.workExperience = (WorkExperience) workExperience.clone();
    }

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

    public void setWorkExperience(String workDate, String company){
        workExperience.setCompany(company);
        workExperience.setWorkDate(workDate);
    }

    public void display(){
        System.out.println("name = " + name + ",sex = " + sex + ",age = " + age);
        System.out.println("工作经历:" + workExperience.getWorkDate() + "-" + workExperience.getCompany());
    }

    /*@Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }*/

    protected Object clone() throws CloneNotSupportedException {
        Resume resume = new Resume(this.workExperience);
        resume.name = this.name;
        resume.sex = this.sex;
        resume.age = this.age;
        return resume;
    }

}

public class Test {

    public static void main(String[] args) throws CloneNotSupportedException {
        Resume resumeA = new Resume("杨过");
        resumeA.setPersonalInfo("M","29");
        resumeA.setWorkExperience("2020-04-03","阿里巴巴");

        Resume resumeB = (Resume) resumeA.clone();
        resumeB.setWorkExperience("2020-01-01","腾讯");

        Resume resumeC = (Resume) resumeA.clone();
        resumeC.setPersonalInfo("F","24");

        resumeA.display();
        resumeB.display();
        resumeC.display();

        /**
         * 运行结果
         *
         * name = 杨过,sex = M,age = 29
         * 工作经历:2020-04-03-阿里巴巴
         * name = 杨过,sex = M,age = 29
         * 工作经历:2020-01-01-腾讯
         * name = 杨过,sex = F,age = 24
         * 工作经历:2020-04-03-阿里巴巴
         */
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!