
本文共 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 ArrayListsubjectList; 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, ListsubjectList) { 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 { // ...}
������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
