原型模式是指原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象,属于创建型设计模式。而不是通过new关键字来创建对象。
适用场景:
1、类初始化消耗资源较多。
2、new产生的一个对象需要非常繁琐的过程(数据准备、访问权限等)
3、构造函数比较复杂
4、虚幻体中产生大量对象时。
实现方式:
1、浅克隆,实现Cloneable接口即可。浅克隆只复制引用,引用类型只复制指向内存的地址,而没有复制值。修改克隆对象,原型对象的值也会修改,会有问题。
2、深克隆。复制的是值。引用类型,会将指向内存的值也会复制,修改克隆对象的值,不会影响原型对象。一般使用序列化的方式实现。
@Data
public class ConcretePrototype implements Cloneable,Serializable {
private int age;
private String name;
private List<String> hobbies;
@Override
public ConcretePrototype clone() {
try {
return (ConcretePrototype)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
public ConcretePrototype deepCloneHobbies(){
try {
ConcretePrototype result = (ConcretePrototype)super.clone();
result.hobbies = (List)((ArrayList)result.hobbies).clone();
return result;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
public ConcretePrototype deepClone(){
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return (ConcretePrototype)ois.readObject();
}catch (Exception e){
e.printStackTrace();
return null;
}
}
@Override
public String toString() {
return "ConcretePrototype{" +
"age=" + age +
", name='" + name + '\'' +
", hobbies=" + hobbies +
'}';
}
}
优缺点:
优点:
1、性能优良,Java自带的 原型模式基于二进制流的拷贝,比直接new性能优良
2、使用深克隆保持对象的状态
缺点:
1、每一个类都要配置一个克隆方法
2、克隆方法位于类的内部,对已有类进行改造,需要修改代码,违反了开闭原则。
3、深克隆实现复杂,对象存在多重嵌套引用,每个对象都需要支持深克隆。