我们从数据库获取数据到DTO,从DTO传递给VO,这个时候需要将DTO层的数据全部copy到VO中,这种模式就是一种原型模式。 copy的方式是克隆。
案例
- spring中的 scope=’prototype’是一个原型模式,每次创建的时候都是一个新的对象,这个对象会取到原有对象的所有的值。
浅克隆
public class Teacher implements Cloneable {
@Override
protected Object clone() 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() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
public static void main(String[] args) throws CloneNotSupportedException {
Student student=new Student(1,"张三");
Teacher teacher = new 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("克隆后,比较克隆对象改变引用");
System.out.println(teacher.getStudent()+"||||||"+ cloneTeacher.getStudent());
//true
//com.gpxy.clone.Teacher@2503dbd3||||||com.gpxy.clone.Teacher@4b67cf4d
//克隆后,比较克隆对象改变引用
//com.gpxy.clone.Student@7ea987ac||||||com.gpxy.clone.Student@7ea987ac
}
深克隆
将所有的值都克隆,完全是新的一份,实现的方式也比较多。如:序列化,反射等。
public class DeepTeacher implements Cloneable,Serializable{
@Override
public DeepTeacher clone() {
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() {
return name;
}
public void setName(String name) {
this.name = name;
}
public DeepStudent getStudent() {
return student;
}
public void setStudent(DeepStudent student) {
this.student = student;
}
public Date getDate() {
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() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args){
DeepTeacher teacher=new DeepTeacher("teacher1", new DeepStudent(2, "学生"),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
//com.gpxy.clone.DeepStudent@63947c6b---com.gpxy.clone.DeepStudent@312b1dae
//false
由此可以看出deepStudet的地址变了。所以深度克隆是完全一个新的。