设计模式之原型设计模式

原型设计模式

原型设计模式的定义是原型实例指定对象的类型,通过拷贝这些原型来创建新的对象

原型设计的例子

以< >例子为例子:我们求职投简历,经历这一栏在投不同的公司需要修改,我们不要再去创建个人信息、工作经历,
通过拷贝来减少简历中对象的创建。

简历父类

package com.learn.java.javabase.designpattern.prototype;

import lombok.Data;

@Data
public class Resume implements Cloneable {
    private String id;

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

简历的实现类

package com.learn.java.javabase.designpattern.prototype;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@AllArgsConstructor
@Slf4j
public class CloneResume01 extends Resume {
    private String name;
    private String age;
    private String year;
    private String workInfo;
    private Remark remark;

    //    public CloneResume01(Remark remark)
//    {
//        this.remark =remark.clone();
//    }
    public CloneResume01(String age, String name) {
        this.age = age;
        this.name = name;
    }

    public CloneResume01(String age, String name, Remark remark) {
        this.age = age;
        this.name = name;
        this.remark = remark;
    }

    public void setWorkInfo(String year, String workinfo) {
        this.year = year;
        this.workInfo = workinfo;
    }

    public void display() {
        log.info("name = {} ,age = {} , in year {}  {},remark ={}", name, age, year, workInfo, remark.toString());
    }

    @Override
    public CloneResume01 clone() {
        CloneResume01 cloneResume01 =null;
        try {
            cloneResume01 = (CloneResume01) super.clone();
            cloneResume01.setRemark(this.getRemark().clone());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return cloneResume01;
    }
}

简历中的备注类Remark

备注类作为简历的一个属性,因为浅复制只能复制String等基础数据类型的值,但对于其他的引用类型并没有拷贝值,只传递对象的引用地址
因此重写Remark类的clone方法,进行深度复制,并在简历类中调用clone方法完成深度复制。

package com.learn.java.javabase.designpattern.prototype;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Remark implements Cloneable {
    private String remarkValue;

    @Override
    public Remark clone() {
        Remark remark = null;
        try {
            remark = (Remark) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return remark;
    }
    @Override
    public String toString(){
        return remarkValue;
    }

}

测试类

package com.learn.java.javabase.designpattern.prototype;

public class TestProtoType {
    public static void main(String[] args) {
        CloneResume01 jackResume=new CloneResume01("fang","28","2017-2019","comp1"
                ,new Remark("first job"));
        CloneResume01 jackResume02=jackResume.clone();
        //Remark没有实现clone方法,下面这句话会因为是复制新的Remark对象,所以打印的Remark是不一样的
        //jackResume02.setRemark(new Remark("second job"));
        //浅复制和深复制,Remark和Resume实现类两个clone方法没有修改,会因为浅复制吗,导致拷贝的实例的值与原型的值是一样的
        jackResume02.getRemark().setRemarkValue("second job");
        jackResume02.setWorkInfo("2020-now","comp2");
        jackResume.display();
        jackResume02.display();
    }
}

运行结果

22:44:53.007 [main] INFO com.learn.java.javabase.designpattern.prototype.CloneResume01 - name = fang ,age = 28 , in year 2017-2019  comp1,remark =first job
22:44:53.010 [main] INFO com.learn.java.javabase.designpattern.prototype.CloneResume01 - name = fang ,age = 28 , in year 2020-now  comp2,remark =second job

你可能感兴趣的:(设计模式之原型设计模式)