从一个对象创建另外一个可定制的对象,而且不知道任何创建的细节;
package caosc.javaModel.ProtoType;
public interface Prototype extends Cloneable
{
public Object clone();
}
package caosc.javaModel.ProtoType;
public class ProtoTypeModel implements Prototype {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@SuppressWarnings("finally")
public synchronized Object clone() {
Prototype temp = null;
try {
temp = (Prototype) super.clone();
return temp;
} catch (CloneNotSupportedException e) {
System.out.println("Clone failed.");
} finally {
return temp;
}
}
}
package caosc.javaModel.ProtoType;
public class ProtoTypeTest {
public static void main(String[] args) {
ProtoTypeModel prototype = new ProtoTypeModel();
prototype.setId(123);
ProtoTypeModel copytype = (ProtoTypeModel) prototype.clone();
System.out.println(copytype.getId());
}
}