1. 前言
原型模式从根本上来说就是Java中的对象克隆,针对于对象的浅拷贝和深拷贝,Java中提供了两种实现方式:
- 实现Cloneable接口并重写
clone()
- 实现Serializable 接口,通过序列化和反序列化实现深拷贝
相较于前面的单例模式和工厂模式,对象克隆的开销相对较大,它通常主要应用于以下场景:
- 依赖于外部资源或硬件密集型操作进行新对象的创建的场景
- 获取相同对象在相同状态的拷贝而无须进行重复获取状态操作的场景
- 在不确定所属具体类时需要对象的实例的场景
关于Java中对象的克隆,在前面的图解Java中的浅拷贝和深拷贝已经讲述的很清楚了,不了解的可自行查阅。因此,原型模式中所涉及的对象克隆的相关内容这里就不再赘述,而是通过一个例子来回顾下相关的内容,同时加深理解下原型模式。
2. 案例
西斯大帝为了消灭最后的天行者(SkyWalker)并再次卷土重来统治银河系,秘密的在Exegol秘密的建造了一支称为Final Order的军队,军队的主要战力就是歼星舰,每艘歼星舰都是名字和攻击力,以及都配备了一名将军。
首先我们需要创建将军类General:
public class General implements Serializable, Cloneable{
String name;
public General(String name) {
this.name = name;
}
@Override
public String toString() {
return "General{" +
"name='" + name + '\'' +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
General实现了Serializable和Cloneable两个接口。然后创建歼星舰类StarDestroyer:
public class StarDestroyer implements Serializable, Cloneable {
private String name;
private double attack;
private General general;
public StarDestroyer(String name, double attack, General general) {
this.name = name;
this.attack = attack;
this.general = general;
}
@Override
public String toString() {
return "StarDestroyer{" +
"name='" + name + '\'' +
", attack=" + attack +
", general=" + general +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException{
StarDestroyer sd = (StarDestroyer) super.clone();
sd.general = (General) general.clone();
return sd;
}
public Object deepCopy() throws IOException {
StarDestroyer starDestroyer = null;
ByteArrayInputStream bis = null;
ByteArrayOutputStream bos = null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
try{
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(this);
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
starDestroyer = (StarDestroyer) ois.readObject();
return starDestroyer;
} catch (Exception e){
e.printStackTrace();
} finally {
bis.close();
bos.close();
ois.close();
oos.close();
}
return starDestroyer;
}
}
由于西斯大帝的信徒众多,因此歼星舰可以在不同的星球采用不同的技术建造。在掌握了歼星舰的建造技术和信徒后,西斯大帝开始命令信徒全力组建Final Order:
public class Sith {
public static void main(String[] args) throws CloneNotSupportedException, IOException {
StarDestroyer starDestroyer = new StarDestroyer("starDestroyer", 10000.0, new General("Lun"));
StarDestroyer two = (StarDestroyer) starDestroyer.clone();
System.out.println(starDestroyer);
System.out.println(two);
System.out.println(starDestroyer == two);
}
}
首先验收了第一个信徒的工作,发现是合格的,命令他加快步伐:
StarDestroyer{name='starDestroyer', attack=10000.0, general=prototype.General@2dda6444}
StarDestroyer{name='starDestroyer', attack=10000.0, general=prototype.General@5e9f23b4}
false
然后验收了第二个信徒的工作,发现也不错,鼓励他再接再厉
public class Sith {
public static void main(String[] args) throws CloneNotSupportedException, IOException {
StarDestroyer starDestroyer = new StarDestroyer("one", 10000.0, new General("Lun"));
StarDestroyer two = (StarDestroyer) starDestroyer.clone();
StarDestroyer three = (StarDestroyer) starDestroyer.deepCopy();
System.out.println(three);
System.out.println(starDestroyer == three);
}
}
StarDestroyer{name='starDestroyer', attack=10000.0, general=General{name='Lun'}}
false
最后西斯大帝拥有了一支庞大的军队,但黑暗永远无法战胜光明,最后的天行者带领抵抗组织和全银河系的平民打败了西斯大帝,赢得了最后的胜利。