设计模式——原型模式
一、基本概念
1. 定义
原型模式是创建型模式的一种,其特点在于通过“复制”一个已经存在的实例来返回新的实例,而不是新建实例。被复制的实例就是我们所称的“原型”,这个原型是可定制的。
原型模式多用于创建复杂的或者耗时的实例,因为这种情况下,复制一个已经存在的实例使程序运行更高效;或者创建值相等,只是命名不一样的同类数据。
2. 优缺点
优点:
- Java 自带的原型模式基于内存二进制流的复制,在性能上比直接 new 一个对象更加优良;
- 可以使用深克隆方式保存对象的状态,使用原型模式将对象复制一份,并将其状态保存起来,简化了创建对象的过程,以便在需要的时候使用(例如恢复到历史某一状态),可辅助实现撤销操作。
缺点:
- 需要为每一个类都配置一个 clone 方法;
- clone 方法位于类的内部,当对已有类进行改造的时候,需要修改代码,违背了开闭原则;
- 当实现深克隆时,需要编写较为复杂的代码,而且当对象之间存在多重嵌套引用时,为了实现深克隆,每一层对象对应的类都必须支持深克隆,实现起来会比较麻烦。因此,深克隆、浅克隆需要运用得当。
3. 结构
原型模式包含以下主要角色。
- 抽象原型类:规定了具体原型对象必须实现的接口;
- 具体原型类:实现抽象原型类的 clone() 方法,它是可被复制的对象;
- 访问类:使用具体原型类中的 clone() 方法来复制新的对象。
4. UML
Java 中的 Object 类提供了浅克隆的 clone() 方法,具体原型类只要实现 Cloneable 接口就可实现对象的浅克隆,这里的 Cloneable 接口就是抽象原型类。
![原型模式的结构图](https://pic.try-hard.cn/blog/3-1Q114101Fa22.gif)
二、代码实现
原型模式的克隆分为浅克隆和深克隆。
- 浅克隆:创建一个新对象,新对象的属性和原来对象完全相同,对于非基本类型属性,仍指向原有属性所指向的对象的内存地址;
- 深克隆:创建一个新对象,属性中引用的其他对象也会被克隆,不再指向原有对象地址。
浅克隆
浅克隆使用 Cloneable 实现即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| package pers.designPattern.prototype;
import java.util.ArrayList;
class ConcretePrototype implements Cloneable {
/*
* 具体原型类
* */
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return super.toString();
}
}
public class ShallowCloning {
/*
* 实现类
* */
public static void main(String[] args) throws CloneNotSupportedException {
ConcretePrototype concretePrototype = new ConcretePrototype();
ArrayList<ConcretePrototype> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add((ConcretePrototype) concretePrototype.clone());
}
list.forEach(System.out::println);
}
}
|
深克隆
深克隆一般情况下都是通过对象的序列化实现 (需要实现Serializable接口) 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
| package pers.designPattern.prototype;
import java.io.*;
class ConPrototype implements Serializable, Cloneable {
/*
* 需要实现 Serializable, Cloneable 两个接口
* */
private Target target = new Target("小红");
public ConPrototype deepClone() {
ByteArrayInputStream bis = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
// 序列化
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(this);
// 反序列化
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
return (ConPrototype) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
} finally {
try {
bos.close();
bis.close();
oos.close();
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Target getTarget() {
return target;
}
}
class Target implements Serializable, Cloneable {
// 需要被深拷贝的类也必须实现两个接口
public String name;
public Target(String name) {
this.name = name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class DeepCloning {
public static void main(String[] args) {
ConPrototype obj = new ConPrototype();
ConPrototype obj1 = (ConPrototype) obj.deepClone();
// 比较
System.out.println(obj.getTarget().equals(obj1.getTarget()));
}
}
|
参考: