原型模式
通过new产生一个对象需要非常繁琐的数据准备或者访问权限,可以使用原型模式。原型模式就是Java中的克隆技术,以某个对象为原型,复制出新的对象。显然,新的对象具备原型模式的特点。原型模式的优势:效率高(直接克隆,避免了重新执行构造过程步骤)。
克隆和new类似,但是不同于new。new创建新的对象属性用的是默认值,克隆出的对象的属性值完全和原型对象相同。并且克隆出的新对象改变不会影响原型对象。然后,再修改克隆对象的值。
Prototype模式中实现起来最困难的地方就是内存复制操作,所幸在Java中提供了clone()方法替我们做了绝大一部分事情,这里需要注意的是,clone()方法是Object里面的,但是如果需要克隆类的对象,必须实现Cloneable接口。
原型模式深复制和浅复制demo
我们模仿克隆羊多利,实现一下羊的克隆,代码如下:
public class Sheep implements Cloneable{
private String sname;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
//直接调用Object对象的clone()方法
Object obj = super.clone();
//添加以下代码实现深复制(deep clone)
/*Sheep s = (Sheep) obj;
s.birthday = (Date)this.birthday.clone();*/
return obj;
}
public Sheep(String sname, Date birthday) {
this.sname = sname;
this.birthday = birthday;
}
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 date = new Date(123123123L);
Sheep sheep = new Sheep("小羊", date);
Sheep s2 = (Sheep) sheep.clone();
System.out.println(sheep);
System.out.println(sheep.getSname());
System.out.println(sheep.getBirthday());
s2.setSname("小小小小羊");
System.out.println(s2);
System.out.println(s2.getSname());
System.out.println(s2.getBirthday());
}
}
执行结果如下:
可以看到成功克隆了一个“小小小小羊”对象出来。
但是这样的复制存在问题,如果我们在克隆出了“小小小小羊”对象之后,修改“小羊”对象的生日,那么“小小小小羊”对象的生日也将被修改,我们修改代码如下:
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
Date date = new Date(123123123L);
Sheep sheep = new Sheep("小羊", date);
Sheep s2 = (Sheep) sheep.clone();
System.out.println(sheep);
System.out.println(sheep.getSname());
System.out.println(sheep.getBirthday());
//修改"小羊"的生日
date.setTime(12345123L);
System.out.println(sheep.getBirthday());
s2.setSname("小小小小羊");
System.out.println(s2);
System.out.println(s2.getSname());
System.out.println(s2.getBirthday());
}
}
运行结果如下所示:
我们称这样的复制为浅复制,出现这样的问题的原因是,克隆出的对象和原对象指向同一个data对象,浅复制对应的是深复制,深复制出来的对象和原对象指向不同的data对象。
我们将Sheep类中注释的两行代码解开注释,然后再次运行上面的代码,则可以得到如下结果:
可以看到,在我们实现了深复制之后,在克隆出了“小小小小羊”对象之后,修改“小羊”对象的生日,“小小小小羊”对象的生日将不会被修改,说明“小小小小羊”对象和“小羊”对象指向了不同的data对象。
序列化和反序列化实现深复制demo
深复制的实现不止上面一种,我们也可以利用序列化和反序列化来实现深复制,此时我们的Sheep类不仅要实现Cloneable接口,也要实现序列化接口Serializable接口。代码如下:
public class Sheep implements Cloneable, Serializable {
private String sname;
private Date birthday;
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
}
public Sheep(String sname, Date birthday) {
this.sname = sname;
this.birthday = birthday;
}
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 Client3 {
public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
Date date = new Date(123123123L);
Sheep sheep = new Sheep("小羊", date);
System.out.println("小羊对象为:" + sheep);
System.out.println("小羊名字为:" + sheep.getSname());
System.out.println("小羊生日为:" + sheep.getBirthday());
//使用序列化和反序列化实现深复制
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(sheep);
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
//克隆出“小小小小羊”
Sheep s2 = (Sheep) ois.readObject();
date.setTime(12345123L);
System.out.println("小羊生日修改后为:" + sheep.getBirthday());
s2.setSname("小小小小羊");
System.out.println("克隆出的小小小小羊对象为:" + s2);
System.out.println("克隆出的小小小小羊名字为:" + s2.getSname());
System.out.println("克隆出的小小小小羊生日为:" + s2.getBirthday());
}
}
结果如下:
可以看到,序列化和反序列化运行出的结果和上面我们深复制的运行结果相同,说明使用序列化和反序列化成功实现了深复制。
原型模式效率测试
通过new产生一个对象需要非常繁琐的数据准备或者访问权限,可以使用原型模式。那么使用原型模式,和使用new来创建新对象相比,效率如何,我们用以下代码来测试。
public class Computer implements Cloneable{
public Computer(){
try {
//模拟创建对象耗时的过程
Thread.sleep(10);
} catch (InterruptedException e){
e.printStackTrace();
}
}
@Override
protected Object clone() throws CloneNotSupportedException {
Object obj = super.clone();
return obj;
}
}
public class Client4 {
public static void testNew(int size) {
long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
Computer c = new Computer();
}
long end = System.currentTimeMillis();
System.out.println("new的方式创建耗时:" + (end - start) + "ms");
}
public static void testClone(int size) throws CloneNotSupportedException {
long start = System.currentTimeMillis();
Computer c = new Computer();
for (int i = 0; i < size; i++) {
Computer temp = (Computer) c.clone();
}
long end = System.currentTimeMillis();
System.out.println("克隆的方式创建耗时:" + (end - start) + "ms");
}
public static void main(String[] args) throws CloneNotSupportedException {
testNew(1000);
testClone(1000);
}
}
运行结果如下所示:
可以看到使用原型模式克隆对象比使用new来创建对象快了很多。但是在实际应用中,原型模式很少单独出现,一般是和工厂方法模式一起出现,通过clone的方法创建一个对象,然后由工厂方法提供给调用者。spring中bean的创建就使用了单例模式和原型模式。
原型模式类图
原型模式使用场景
Cloneable接口和Clone()
JavaScript中的继承里面的prototype
以上为原型模式的学习笔记,此文章为尚学堂视频的学习笔记+自己总结。