1.使用场景
<1>通过new产生一个对象需要非常繁琐的数据准备或访问权限,可以使用原型模式
<2>java中的克隆技术以某个对象为原型,复制出新的对象,显然新的对象具备原对象的特点。克隆类似于new,但是不同于new。new创建新的对象属性采用的是默认值。克隆出的对象的属性值完全和原型对象相同
2.优势
效率高(直接克隆,避免了重新执行构造过程的步骤)
3.实现
对象实现Cloneable接口(标志类接口),实现Object中的克隆方法
原型模式中实现最困难的地方是内存复制操作
4.代码
<1>浅克隆
//对象
public class Sheep implements Cloneable {
private String name;
private Date birthday;
public Sheep(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object object=super.clone();
return object;
}
}
//客户端
public class ClientSimple {
public static void main(String[] args) throws CloneNotSupportedException {
Date date=new Date(12132312312l);
Sheep sheep=new Sheep("少利", date);
System.out.println(sheep);
System.out.println(sheep.getName());
System.out.println(sheep.getBirthday());
Sheep sheep2=(Sheep)sheep.clone();
date.setTime(23456712312312l);
System.out.println(date);
System.out.println(sheep2);
System.out.println(sheep2.getName());
System.out.println(sheep2.getBirthday());
}
}
//执行结果
com.test.pattern.Sheep@6c22e349
少利
Thu May 21 18:05:12 CST 1970
Sat Apr 26 01:25:12 CST 2713
com.test.pattern.Sheep@575887db
少利
Sat Apr 26 01:25:12 CST 2713
<2>深克隆
```
//对象
public class SheepDeep implements Cloneable {
private String name;
private Date birthday;
public SheepDeep(String name, Date birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object object=super.clone();
SheepDeep sheepDeep=(SheepDeep)object;
sheepDeep.birthday=(Date)this.birthday.clone();
return sheepDeep;
}
}
//客户端
public class ClientDeep {
public static void main(String[] args) throws CloneNotSupportedException {
Date date=new Date(123456789l);
SheepDeep sheepDeep=new SheepDeep(“少利”, date);
SheepDeep sheepDeep2=(SheepDeep)sheepDeep.clone();
System.out.println(sheepDeep);
System.out.println(sheepDeep.getName());
System.out.println(sheepDeep.getBirthday());
date.setTime(1234567123l);
System.out.println(sheepDeep.getBirthday());
sheepDeep2.setName(“多利”);
System.out.println(sheepDeep2);
System.out.println(sheepDeep2.getName());
System.out.println(sheepDeep2.getBirthday());
}
}
//执行结果
com.test.pattern.SheepDeep@5195da41
少利
Fri Jan 02 18:17:36 CST 1970
Thu Jan 15 14:56:07 CST 1970
com.test.pattern.SheepDeep@15aec462
多利
Fri Jan 02 18:17:36 CST 1970
“`
利用序列化和反序列化实现深度克隆
public class ClientSerializable {
public static void main(String[] args) throws Exception {
Date date=new Date(12345678l);
Sheep s1=new Sheep("少利", date);
System.out.println(s1);
System.out.println(s1.getName());
System.out.println(s1.getBirthday());
//序列化方式实现深克隆
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos);
oos.writeObject(s1);
byte[] byteArray=bos.toByteArray();
//反序列化
ByteArrayInputStream bis=new ByteArrayInputStream(byteArray);
ObjectInputStream ois=new ObjectInputStream(bis);
Sheep s2=(Sheep)ois.readObject();
date.setTime(1111111111111l);
System.out.println("s1 birthday:"+s1.getBirthday());
s2.setName("多利");
System.out.println(s2);
System.out.println(s2.getName());
System.out.println(s2.getBirthday());
}
}
//执行结果
com.test.pattern.Sheep@59c527be
少利
Thu Jan 01 11:25:45 CST 1970
s1 birthday:Fri Mar 18 09:58:31 CST 2005
com.test.pattern.Sheep@63d6dc46
多利
Thu Jan 01 11:25:45 CST 1970