设计模式 -- 原型模式 图解java对象克隆 引用拷贝、浅拷贝、深拷贝、序列化拷贝
发布日期:2021-05-14 12:39:19 浏览次数:24 分类:精选文章

本文共 3630 字,大约阅读时间需要 12 分钟。

���������������������������

���������������Prototype Pattern������������������������������������������������������������������������������������������������Clone���������������������������������������������������������������������������������������������������������������������������������������������

���������������������������

������������������������������������new ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������

������������������������������Cloneable���������������clone���������������������Java������Date������clone������������������������������������

public Object clone() {
Date d = null;
try {
d = (Date) super.clone();
if (cdate != null) {
d.cdate = (BaseCalendar.Date) cdate.clone();
}
} catch (CloneNotSupportedException e) {}
return d;
}

���������������������

������������������������������������������������������������������������������������������������������������������������������

1. ���������

���������������������������������������������������������������������������������������������������

Student zhangsan = new Student("������", 15, new ArrayList<>());
Student copy = zhangsan;
copy.setName("������"); // ������������������������������������������������

������������������������������������������������������������������������������������������������

2. ���������

���������������������������������������������������������������������������������

public class Student implements Cloneable {
private final String name;
private final int age;
private final ArrayList
subjectList;
public Student(String name, int age, ArrayList
subjectList) {
this.name = name;
this.age = age;
this.subjectList = subjectList;
}
// ... (������������)
@Override
protected Object clone() throws CloneNotSupportedException {
return new Student(this.name, this.age, new ArrayList<>(this.subjectList));
}
}

������������������������������������������������������������������������������������������������������������

3. ������������������

������������������������������������������������������������������������������Serializable������������������������������������

public class Student implements Serializable {
// ... (������������)
private static final long serialVersionUID = 123456789123456789L;
public Student(String name, int age, List
subjectList) {
this.name = name;
this.age = age;
this.subjectList = new ArrayList<>(subjectList);
}
// ... (������������)
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
this.subjectList = new ArrayList<>(in.readObject());
}
}

���������������������������������������������������������������������������������������������������

���������������Spring

���Spring��������������������� PU���Prototype Scope���������������������bean���������������������������������������������������������������������������������������������������@Scope("prototype")���������

@Service
@Scope("prototype")
public class StudentService {
// ...
}

������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:设计模式 -- 外观模式(门面模式)
下一篇:设计模式 -- 适配器模式

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月10日 02时47分51秒