原型模式,个人觉得比较简单,因为java已经为我们提供好接口,可以轻松的实现对象的copy.
public class Clone implements Cloneable{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int i) {
this.id = i;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object clone() {
Object obj = null;
try {
obj = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return obj;
}
}
public class CloneClient {
public static void main(String[] args){
Clone c = new Clone();
c.setId(1);
c.setName("num1");
Clone c2 =(Clone) c.clone();
c2.setId(2);
System.out.print("num1 :"+c.getId() + "; num2 :"+c2.getId());
}
}