设计模式-03.原型模式
原型模式
原型模式(Prototype Pattern)是创建型模式,用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式。
这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。
调用者不需要知道任何创建细节,不需要调用构造函数。
一般情况下原型模式基于二进制流的复制,不是简单地set,get。
- 掌握原型模式的应用场景
- 掌握浅克隆和深克隆的写法
- 了解克隆模式是如何破坏单例的
- 了解原型模式的优缺点
IPrototype.java
public interface IPrototype<T> {
T clone();
}
ConcretePrototype.java
public class ConcretePrototype implements IPrototype {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public ConcretePrototype clone() {
ConcretePrototype concretePrototype = new ConcretePrototype();
concretePrototype.setAge(this.age);
concretePrototype.setName(this.name);
return concretePrototype;
}
@Override
public String toString() {
return "ConcretePrototype{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
Client.java
public class Client {
public static void main(String[] args) {
//创建原型对象
ConcretePrototype prototype = new ConcretePrototype();
prototype.setAge(18);
prototype.setName("Tom");
System.out.println(prototype);
//拷贝原型对象
ConcretePrototype cloneType = prototype.clone();
System.out.println(cloneType);
}
}
应用场景
- 类初始化消耗资源较多。
- new 产生的一个对象需要非常繁琐的过程(数据准备、访问权限等)
- 构造函数比较复杂
- 循环体中生产大量对象。
优点
- 性能优良,java自带的原型模式是基于内存二进制流的拷贝,比直接new一个对象性能上提升了很多。
- 可以使用深克隆的方式保存对象状态,使用原型模式将对象复制一份并将其状态保存起来,简化了创建过程。
缺点
- 必须配备克隆(可拷贝)方法
- 当对已有类进行改造的时候,需要需修改代码,违反了开闭原则。
- 深克隆(拷贝)、浅拷贝(拷贝)需要运用得当
- 原型模式跟单例模式同一类内不共存
浅克隆
ConcretePrototype.java
//实现jdk的 Cloneable 接口
@Data
public class ConcretePrototype implements Cloneable {
private int age;
private String name;
private List<String> hobbies;
//重写Cloneable接口中的clone方法,clone方法默认实现的是浅克隆
@Override
public ConcretePrototype clone() {
try {
return (ConcretePrototype)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
@Override
public String toString() {
return "ConcretePrototype{" +
"age=" + age +
", name='" + name + '\'' +
", hobbies=" + hobbies +
'}';
}
}
缺点
当原型对象中有引用类型的属性时,修改克隆对象的引用类型属性,原型对象中的对应属性也会被修改。
深克隆
实现方式1:
ConcretePrototype.java
@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 deepClone(){
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//转为java可识别的字节流
ObjectOutputStream oos = new ObjectOutputStream(bos);
//不写入磁盘,this在内存中操作
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
//readObject通过反射newInstance(通过反射new一个新的对象)
return (ConcretePrototype)ois.readObject();
}catch (Exception e){
e.printStackTrace();
return null;
}finally{
oos.close();
ois.close();
}
}
@Override
public String toString() {
return "ConcretePrototype{" +
"age=" + age +
", name='" + name + '\'' +
", hobbies=" + hobbies +
'}';
}
}
实现方式2:
利用Json,先把对象转为Json字符串再把Json串转为对象
public Prototype JsonClone(){
Prototype prototype = this;
Prototype obj = JSON.toJavaObject((JSON)JSONObject.toJSON(prototype),Prototype.class);
return obj;
}
缺点/局限
-
原型模式与单例模式不在同一个类共存
-
若原型对象为单例,使用克隆后破坏单例模式,因为单例构造方法私有化,而克隆时不通过构造法创建对象。
解决:
- 单例模式不能实现Cloneable接口
- 单例模式跟原型模式冲突,二者只能用其一。
实例
- ArrayList
- HashMap
总结
- 实现Cloneable接口都是浅克隆,ArrayList中若存在引用类型的属性时也会存在浅克隆的问题。
- 可以通过序列化和转Json来实现深克隆
- apache.lang.util,spring,jdk中都提供了相关的对象操作方法