大话设计模式读书笔记——原型模式


1. 需求

设计一个一个简历类,必须要有姓名,可以设置性别和年龄,可以设置工作经历。最终需要写三份简历。

2. 代码版本1.0

2.1 简历类

public class Resume {
    private String name;
    private String sex;
    private String age;
    private String timeArea;
    private String company;

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

    //设置个人信息
    public void setPersonalInfo(String sex,String age){
        this.sex = sex;
        this.age = age;
    }

    //设置工作经历
    public void setWorkExperience(String timeArea,String company){
        this.timeArea = timeArea;
        this.company = company;
    }

    //展示简历
    public void display(){
        System.out.println(this.name+" "+this.sex+" "+this.age);
        System.out.println("工作经历"+this.timeArea+" "+this.company);
    }
}

2.2 客户端

Resume resume1 = new Resume("大鸟");
        resume1.setPersonalInfo("男","29");
        resume1.setWorkExperience("1998-2000","xx公司");

        Resume resume2 = new Resume("大鸟");
        resume2.setPersonalInfo("男","29");
        resume2.setWorkExperience("1998-2000","xx公司");

        Resume resume3 = new Resume("大鸟");
        resume3.setPersonalInfo("男","29");
        resume3.setWorkExperience("1998-2000","xx公司");

        resume1.display();
        resume2.display();
        resume3.display();

2.3 运行结果

大鸟 男 29
工作经历1998-2000 xx公司
大鸟 男 29
工作经历1998-2000 xx公司
大鸟 男 29
工作经历1998-2000 xx公司

2.4 弊端

三份简历需要三次实例化。如果要二十份,就需要二十次实例化。因此,我们需要原型模式

3. 原型模式

3.1 概念

原型模式(Prototype),用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象

3.2 UML类图

在这里插入图片描述

3.3 案例代码

3.3.1 原型类

public class Prototype implements Cloneable{
    private String id;

    public Prototype(String id){
        this.id = id;
    }

    public String getID(){
        return this.id;
    }

    @Override
    protected Object clone() {
        Object object = null;
        try {
            object = super.clone();
        }catch (CloneNotSupportedException e){
            System.err.println("Clone异常。");
        }
        return object;
    }
}

3.3.2 具体原型类

public class ConcretePrototype extends Prototype{
    public ConcretePrototype(String id){
        super(id);
    }
}

3.3.3 客户端

	ConcretePrototype p1 =new ConcretePrototype("编号");
    System.out.println("原ID:"+p1.getID());

    ConcretePrototype c1 = (ConcretePrototype) p1.clone();
    System.out.println("原ID:"+c1.getID());

4. 代码版本2.0(原型模式实现简历功能)

现在使用原型模式实现简历功能。

4.1 UML结构图

在这里插入图片描述

4.2 简历类

public class Resume implements Cloneable{
    private String name;
    private String sex;
    private String age;
    private String timeArea;
    private String company;
    
    public Resume(String name){
        this.name = name;
    }
    
    ...//省略一部分代码

    protected Resume clone(){
        Resume object = null;
        try {
            object = (Resume) super.clone();
        }catch (CloneNotSupportedException e){
            System.err.println("Clone异常");
        }
        return object;
    }
}

4.3 客户端类

        Resume resume1 = new Resume("大鸟");
        resume1.setPersonalInfo("男","29");
        resume1.setWorkExperience("1998-2000","XX公司");

        Resume resume2 = resume1.clone();
        resume2.setWorkExperience("2000-2003","YY集团");

        Resume resume3 = resume1.clone();
        resume3.setPersonalInfo("男","24");

        resume1.display();
        resume2.display();
        resume3.display();

4.4 总结

原型模式的使用,不用重新初始化对象,而是动态地获得对象运行时的状态。这既隐藏了对象创建的细节,又对性能是大大的提高。

5. 深复制和浅复制

5.1 需求

现在的简历类当中有一个设置工作经历的方法,在现实设计当中,一般会再有一个工作经历类,当中有时间区间公司名称等属性,简历类直接调用这个对象即可。

5.2 代码版本3.0

5.2.1 UML 类图

在这里插入图片描述

5.2.2 工作经历类

public class WorkExperience{
    //工作时间范围
    private String timeArea;
    public String getTimeArea(){
        return this.timeArea;
    }
    public void setTimeArea(String value){
        this.timeArea = value;
    }

    //所在公司
    private String company;
    public String getCompany(){
        return this.company;
    }
    public void setCompany(String value){
        this.company = value;
    }
}

5.2.3 简历类

//简历类
public class Resume implements Cloneable{
    private String name;
    private String sex;
    private String age;
    private WorkExperience work;    //声明一个工作经历的对象
    public Resume(String name){
        this.name = name;
        this.work = new WorkExperience();//对这个工作经历对象实例化
    }
    //设置个人信息
    public void setPersonalInfo(String sex,String age){
        this.sex = sex;
        this.age = age;
    }

    //摄制工作经历
    public void setWorkExperience(String timeArea,String company){
        this.work.setTimeArea(timeArea);
        this.work.setCompany(company);
    }

    //展示简历
    public void display(){
        System.out.println(this.name+" "+this.sex+" "+this.age);
        System.out.println("工作经历"+this.work.getTimeArea()+" "+this.work.getCompany());
    }

    public Resume clone(){
        Resume object = null;
        try {
            object = (Resume) super.clone();
        }catch (CloneNotSupportedException e){
            System.err.println("Clone异常");
        }
        return object;
    }
}

5.2.4 客户端类

        Resume resume1 = new Resume("大鸟");
        resume1.setPersonalInfo("男","29");
        resume1.setWorkExperience("1998-2000","XX公司");

        Resume resume2 = resume1.clone();
        resume2.setWorkExperience("2000-2003","YY集团");

        Resume resume3 = resume1.clone();
        resume3.setPersonalInfo("男","24");

        resume1.display();
        resume2.display();
        resume3.display();

5.2.5 输出结果

大鸟 男 29
工作经历2000-2003 YY集团
大鸟 男 29
工作经历2000-2003 YY集团
大鸟 男 24
工作经历2000-2003 YY集团

结果显示,前两次的工作经历数据被最后一次数据给覆盖了。这是因为clone方法是浅复制,被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。
但我们可能更需要这样的一种需求,把要复制的对象所引用的对象都复制一遍。这种叫做深复制,深复制把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象

5.3 代码版本4.0

5.3.1 工作经历类

public class WorkExperience implements Cloneable{
    //工作时间范围
    private String timeArea;
    public String getTimeArea(){
        return this.timeArea;
    }
    public void setTimeArea(String value){
        this.timeArea = value;
    }

    //所在公司
    private String company;
    public String getCompany(){
        return this.company;
    }
    public void setCompany(String value){
        this.company = value;
    }
    
    protected WorkExperience clone() {
        WorkExperience object = null;
        try {
            object = (WorkExperience) super.clone();
        }catch (CloneNotSupportedException e){
            System.err.println("Clone异常");
        }
        return object;
    }
}

5.3.2 简历类

package com.lzh.prototype.v4;

public class Resume {
    private String name;
    private String sex;
    private String age;
    private WorkExperience work;    //声明一个工作经历的对象
    public Resume(String name){
        this.name = name;
        this.work = new WorkExperience();//对这个工作经历对象实例化
    }
    //设置个人信息
    public void setPersonalInfo(String sex,String age){
        this.sex = sex;
        this.age = age;
    }

    //摄制工作经历
    public void setWorkExperience(String timeArea,String company){
        this.work.setTimeArea(timeArea);
        this.work.setCompany(company);
    }

    //展示简历
    public void display(){
        System.out.println(this.name+" "+this.sex+" "+this.age);
        System.out.println("工作经历"+this.work.getTimeArea()+" "+this.work.getCompany());
    }

    public Resume clone(){
        Resume object = null;
        try {
            object = (Resume) super.clone();
            object.work = this.work.clone();
        }catch (CloneNotSupportedException e){
            System.err.println("Clone异常");
        }
        return object;
    }
}

5.3.3 输出结果

客户端类同之前一样,输出结果如下。

大鸟 男 29
工作经历1998-2000 XX公司
大鸟 男 29
工作经历2000-2003 YY集团
大鸟 男 24
工作经历1998-2000 XX公司

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

头盔程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值