原型模式
- 通过new产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式
- 就是java中的克隆技术,以某个对象为原型,复制出新的对象,显然,新的对象具备原型对象的特点
- 优势有:效率高可以直接克隆,避免了重新执行构造过程步骤
- 克隆类似于new,但是不同于new。nei创建新的对象属性采用的是默认值,克隆出的对象的属性值完全和原型对象相同,并且克隆出的新对象改变不会影响原型对象,然后再修改克隆对象的值。
原型模式实现方式
- Cloneable接口和clone方法
- Prototype模式中实现起来最困难的地方就是内存复制操作,所幸在Java中提供了clone()方法替我们做了绝大部分事情
浅克隆
public class Sheep implements Cloneable {
private String sname ; //1997年英国多利诞生
private Date birthday ;
public Sheep(String sname, Date birthday){
super();
this.sname = sname;
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();//返回羊对象
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
Date date1 = new Date();
Sheep xyy = new Sheep("喜洋洋",date1);
Sheep doly = (Sheep)xyy.clone();
date1.setTime(123123123L); //date1 变 xyy 与 doly的birthday都跟着改变 。。。。浅拷贝
System.out.println(xyy);
System.out.println(doly);
System.out.println(xyy.getSname());
System.out.println(xyy.getBirthday());
System.out.println(doly.getSname());
System.out.println(doly.getBirthday());
}
}
深克隆
public class Sheep2 implements Cloneable {
private String sname ; //1997年英国多利诞生
private Date birthday ;
public Sheep2(String sname, Date birthday){
super();
this.sname = sname;
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
//添加如下代码实现深复制
Sheep2 s = ((Sheep2) obj);
s.birthday = (Date)this.birthday.clone();
return s;//返回羊对象
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
public class DeepClient {
public static void main(String[] args) throws CloneNotSupportedException {
Date date1 = new Date();
Sheep2 xyy = new Sheep2("喜洋洋", date1);
Sheep2 doly = (Sheep2)xyy.clone();
date1.setTime(123123123L);
// doly.setSname("多利");
// doly.setBirthday(date1);
System.out.println(xyy);
System.out.println(doly);
System.out.println(xyy.getSname());
System.out.println(xyy.getBirthday());
System.out.println(doly.getSname());
System.out.println(doly.getBirthday());
}
}
利用序列化和反序列化实现深克隆
需要实现Serializable接口
public class Sheep implements Cloneable,Serializable {
private String sname ; //1997年英国多利诞生
private Date birthday ;
public Sheep(String sname, Date birthday){
super();
this.sname = sname;
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();//返回羊对象
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
通过 IO 流来实现深度拷贝
/**
* 使用序列化和反序列化的方式实现深克隆
*/
public class Client3 {
public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
Date date1 = new Date();
Sheep xyy = new Sheep("喜洋洋",date1);
System.out.println("喜洋洋修改之前的参数");
System.out.println(xyy);
System.out.println(xyy.getSname());
System.out.println(xyy.getBirthday());
//doly 通过反序列接口获得
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(xyy);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
Sheep doly = (Sheep)ois.readObject();
System.out.println("修改之后喜洋洋的参数");
date1.setTime(123123123L); //date1 变 xyy发生变化
System.out.println(xyy);
System.out.println(xyy.getSname());
System.out.println(xyy.getBirthday());
System.out.println("修改之后多利的参数"); //深拷贝之后doly不变
System.out.println(doly);
System.out.println(doly.getSname());
System.out.println(doly.getBirthday());
}
}
- 如果需要短时间创建大量的对象,并且通过new的过程比较耗时,可以考虑原型模式,新建普通new方式创建对象和clone方式创建对象的效率差异
- 原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone方法创建一个对象然后由工厂方法提供给调用者
- spring中bean的创建实际就是两种:单例模式和原型模式,当然原型模式需要和工厂模式搭配使用适配器模式