设计模式-原型模式

原型模式

  • 用原型类型指定创建对象的种类,并通过拷贝这些原型创建新的对象。
  • 原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。
  • 代码框架如下
	// 原型类
	class ConcretePrototype1 extends  implements Cloneable
	{
		private String id;
		public Prototype(String id)
		{
			this.id=id;
		}	
		public String getId(){
			return this.id;
		}
		@Override
		protected Object clone() throws CloneNotSupportedException{
			return (Prototype)super.clone();
		}
	}
  • 对于简历的实现
public class Resume implements Cloneable{
    private String name;
    private String sex;
    private String age;
    private String timeArea;
    private String company;

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

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

    public String getTimeArea() {
        return timeArea;
    }

    public void setTimeArea(String timeArea) {
        this.timeArea = timeArea;
    }

    public String getCompany() {
        return company;
    }

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

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

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

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

    public static void main(String[] args) throws CloneNotSupportedException {
        Resume a = new Resume("大鸟","男","18","2000-2002","阿里巴巴");
		//只需要调用clone方法就可以实现新简历的生成,并且可以在修改新简历的细节
        Resume b = (Resume)a.clone();
        b.setCompany("腾讯");

        Resume c = (Resume)a.clone();
        c.setCompany("百度");
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}
  • 一般在初始化的信息不发生变化的情况下,克隆是最好的方法,既隐藏了对象创建的细节,又对性能是大大的提升
  • 等于是不用重新初始化对象,而是动态地获得对象运行时的状态。

浅复制与深复制

浅复制

//原型模式
class WorkExperience{
    private String timeArea;
    private String company;


    @Override
    public String toString() {
        return "WorkExperience{" +
                "timeArea='" + timeArea + '\'' +
                ", company='" + company + '\'' +
                '}';
    }

    public String getTimeArea() {
        return timeArea;
    }

    public void setTimeArea(String timeArea) {
        this.timeArea = timeArea;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }
}
public class Resume implements Cloneable{
    private String name;
    private String sex;
    private String age;
    private WorkExperience workExperience;

    public WorkExperience getWorkExperience() {
        return workExperience;
    }

    public void setWorkExperience(String timeArea, String company) {
        workExperience.setTimeArea(timeArea);
        workExperience.setCompany(company);
    }

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

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


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

    public static void main(String[] args) throws CloneNotSupportedException {

        Resume a = new Resume("大鸟","男","18");
        a.setWorkExperience("2000-2002","阿里巴巴");
        Resume b = (Resume)a.clone();
        a.setWorkExperience("2000-2002","百度");

        Resume c = (Resume)a.clone();
        a.setWorkExperience("2000-2002","腾讯");
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

在这里插入图片描述

  • 查看上面结果,发现仅仅是对Resume对象进行了复制,而对于WorkExperience对象没有,即还是操作的同一个WorkExperience对象。
  • 如果字段是值类型的,则对该字段执行逐位复制,如果字段是引用类型,则复制引用而不复制引用的对象:即原始对象及其副本引用属于同一对象。
  • 这种现象叫做浅复制:被复制对象的所有变量都含有与原来对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。

深复制

  • 深复制把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。
//原型模式
class WorkExperience implements Cloneable{
    private String timeArea;
    private String company;


    @Override
    public String toString() {
        return "WorkExperience{" +
                "timeArea='" + timeArea + '\'' +
                ", company='" + company + '\'' +
                '}';
    }

    public String getTimeArea() {
        return timeArea;
    }

    public void setTimeArea(String timeArea) {
        this.timeArea = timeArea;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        return (WorkExperience)super.clone();
    }
}
public class Resume implements Cloneable{
    private String name;
    private String sex;
    private String age;
    private WorkExperience workExperience;
	// 提供clone()方法调用的私有构造函数,克隆该对象
    private Resume(WorkExperience workExperience) throws CloneNotSupportedException {
        this.workExperience = (WorkExperience)workExperience.clone();
    }
	
    public WorkExperience getWorkExperience() {
        return workExperience;
    }

    public void setWorkExperience(String timeArea, String company) {
        workExperience.setTimeArea(timeArea);
        workExperience.setCompany(company);
    }

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

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

	//重写clone()方法,对“WorkExperience”对象进行克隆,然后相关字段赋值
    @Override
    public 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 static void main(String[] args) throws CloneNotSupportedException {

        Resume a = new Resume("大鸟","男","18");
        a.setWorkExperience("2000-2002","阿里巴巴");
        Resume b = (Resume)a.clone();
        a.setWorkExperience("2000-2002","百度");

        Resume c = (Resume)a.clone();
        a.setWorkExperience("2000-2002","腾讯");
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

结果如下:深复制完成
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值