java 之 原型模式(大话设计模式)

原型模式,在笔者理解看来就是克隆,当我们在创建第一个对象时,已经给对象赋值完毕,此时我们需要一个当前对象的副本,如果没有原型模式,我们会再次创建一个对象,然后后二次赋值,保证两个对象完全一致,

这样我们的代码可读性就比较差,而且稍不留神copy错了麻烦就大了了,此时我们如果了解原型模式,就能够美化我们的代码,看起来够整洁,而且也不会出错。

大话设计模式-类图-浅复制

所谓的浅复制,就是对象中不包含引用类型,学习JAVA的小伙伴都知值引用和地址引用的区别吧,这里不再多说,如果还不了解的小伙伴可以自行百度下区别。

先上浅复制代码:

/**
 *  简历对象
 */
public class Resume implements Cloneable{

    private String name;

    private String age;

    private String company;

    public Resume() {
    }

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

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

}
/**
 * 客户端
 */
public class Test {

    public static void main(String[] args) throws CloneNotSupportedException {
        Resume resume = new Resume("小菜", "25");
        resume.setCompany("XXX-公司");
        Resume resume1 = (Resume) resume.clone();
        resume1.setCompany("AAA-公司");
        System.out.println(resume.getName());
        System.out.println(resume.getCompany());
        System.out.println(resume1.getName());
        System.out.println(resume1.getCompany());
    }
}

输出结果:

小菜
XXX-公司
小菜
AAA-公司

 以上是浅复制,均是String类型,现在需求有变更,工作经历属性变多,我们需要分对象,简历类里包含工作经验,显然浅复制无法复制引用类型,这里我们引入深复制

深复制代码如下:

/**
 *  简历对象
 */
public class Resume implements Cloneable{

    private String name;

    private String age;

    private WorkExperience workExperience;

    public Resume() {
        workExperience = new WorkExperience();
    }

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

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Resume resume = (Resume) super.clone();
        resume.setWorkExperience((WorkExperience) this.workExperience.clone());
        return resume;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public WorkExperience getWorkExperience() {
        return workExperience;
    }

    public void setWorkExperience(WorkExperience workExperience) {
        this.workExperience = workExperience;
    }
}
/**
 * 客户端
 */
public class Test {

    public static void main(String[] args) throws CloneNotSupportedException {
        Resume resume = new Resume("小菜", "25");
        resume.getWorkExperience().setCompany("XXX-公司");

        Resume resume1 = (Resume) resume.clone();
        resume1.getWorkExperience().setCompany("AAA-公司");
        System.out.println(resume.getName());
        System.out.println(resume.getWorkExperience().getCompany());
        System.out.println(resume1.getName());
        System.out.println(resume1.getWorkExperience().getCompany());
    }
}

输出结果:

小菜
XXX-公司
小菜
AAA-公司

以上是笔者对原型模式的理解,希望能帮助学习原型模式的童鞋!

posted @ 2017-11-28 19:04  红尾巴的猪  阅读(632)  评论(1编辑  收藏  举报