Prototype mode: We obtain data from database to DTO and transfer data from DTO to VO. At this time, we need to copy all data of DTO layer into VO. This mode is a prototype mode.

The way of copy is cloning.


Example: Scope =’prototype’ in Spring is a prototype pattern. Each time a new object is created, this object takes all the values of the original object.

Shallow cloning:

public class Teacher implements Cloneable {

    @Override
    protected Object clone(a) throws CloneNotSupportedException {
        return super.clone();
    }
    public Teacher(String name, Student student, Date date) {
        this.name = name;
        this.student = student;
        this.date = date;
    }
    private String name;
    private Student student;
    private Date date;
    public String getName(a) {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Student getStudent(a) {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public Date getDate(a) {
        return date;
    }
    public void setDate(Date date) {
        this.date = date; }}public static void main(String[] args) throws CloneNotSupportedException {

       Student student=new Student(1."Zhang");
        Teacher teacher = new Teacher("Teacher",  student,new Date());

        Teacher cloneTeacher =(Teacher) teacher.clone();
        System.out.println(teacher.getStudent().getClass() == cloneTeacher.getStudent().getClass());
        System.out.println(teacher+"| | | | | |"+cloneTeacher);


        System.out.println("After cloning, compare clone object change reference");
        System.out.println(teacher.getStudent()+"| | | | | |"+ cloneTeacher.getStudent());
//true
//com.gpxy.clone.Teacher@2503dbd3||||||com.gpxy.clone.Teacher@4b67cf4d
// After cloning, compare cloned objects to change references
//com.gpxy.clone.Student@7ea987ac||||||com.gpxy.clone.Student@7ea987ac

    }
    
Copy the code

Deep cloning:

All the values are cloned, is a completely new copy, more ways to achieve. Such as: serialization, reflection, etc.

public class DeepTeacher implements Cloneable.Serializable{

    @Override
    public DeepTeacher clone(a)  {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(this);

            ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream);
            DeepTeacher o = (DeepTeacher)objectInputStream.readObject();
            return o;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public DeepTeacher(String name, DeepStudent student, Date date) {
        this.name = name;
        this.student = student;
        this.date = date;
    }
    private String name;
    private DeepStudent student;
    private Date date;
    public String getName(a) {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public DeepStudent getStudent(a) {
        return student;
    }
    public void setStudent(DeepStudent student) {
        this.student = student;
    }
    public Date getDate(a) {
        return date;
    }
    public void setDate(Date date) {
        this.date = date; }}public class DeepStudent implements Cloneable.Serializable {

    private Integer id;
    private String name;
    public DeepStudent(Integer id, String name) {
        this.id = id;
        this.name = name;
    }
    public Integer getId(a) {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName(a) {
        return name;
    }
    public void setName(String name) {
        this.name = name; }}public static void main(String[] args){
        DeepTeacher teacher=new DeepTeacher("teacher1".new DeepStudent(2."Students"),new Date());
        DeepTeacher cloneDeepTeacher = teacher.clone();
        System.out.println(teacher+"--"+cloneDeepTeacher);
        System.out.println(teacher.getStudent()+"-"+cloneDeepTeacher.getStudent());
        System.out.println(teacher==cloneDeepTeacher);
    }
//com.gpxy.clone.DeepTeacher@610455d6----com.gpxy.clone.DeepTeacher@27973e9b
//[email protected]@312b1dae
//false

Copy the code

This shows that the address of deepStudet has changed. So deep cloning is completely new.

Pay attention to the public number, wonderful continue