原型模式
其实也是克隆模式,他就是对原有的对象实例进行一次拷贝。但是对象不同,对象其中的属性都相同。
/**
* Created by malei on 2016/12/5.
*/
public class Cat implements Cloneable{
private String name;
private int age;
private Date birthday;
public Cat(String name,int age,Date birthday){
this.name=name;
this.age=age;
this.birthday=birthday;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Date getBirthday() {
return birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone(); //直接使用父类的克隆方法
//添加如下代码实现深复制
Cat s = (Cat) obj;
//将属性中的对象(非基本数据类型)也进行拷贝
s.birthday = (Date) this.birthday.clone();
return obj;
}
public static void main(String[] args) throws CloneNotSupportedException {
Cat cat = new Cat("tom",12,new Date());
Log.show(cat.getName());
//克隆一个对象,对象不同,但属性值相同
Cat cat2 = (Cat)cat.clone();
Log.show("cat和cat2是否相等:" + (cat==cat2)); //cat和cat2是否相等:false
Log.show(cat2.getName()); //tom
Log.show(cat2.getAge()); //12
Log.show(cat2.getBirthday()+""); //Mon Dec 05 22:31:35 CST 2016
Log.show(cat.getBirthday()+""); //Mon Dec 05 22:31:35 CST 2016
}
}