意图
原型模式(Prototype)是一种创建型设计模式,使你能够复制已有对象,而又无需使代码依赖它们所属的类。
解决方案
原型模式将克隆过程委派给被克隆的实际对象。模式为所有支持克隆的对象声明了一个通用接口,该接口让你能够克隆对象,同时又无需将代码和对象所属类耦合。通常情况下,这样的接口中仅包含一个 克隆
方法。
所有的类对 克隆
方法的实现都非常相似。该方法会创建一个当前类的对象,然后将原始对象所有的成员变量值复制到新建的类中。你甚至可以复制私有成员变量,因为绝大部分编程语言都允许对象访问其同类对象的私有成员变量。
支持克隆的对象即为原型。当你的对象有几十个成员变量和几百种类型时,对其进行克隆甚至可以代替子类的构造。
其运作方式如下:创建一系列不同类型的对象并用不同的方式对其进行配置。如果所需对象与预先配置的对象相同,那么你只需克隆原型即可,无需新建一个对象。
原型模式结构
基本实现
原型(Prototype)接口将对克隆方法进行声明。在绝大多数情况下,其中只会有一个名为 clone
克隆的方法。
具体原型(Concrete Prototype)类将实现克隆方法。除了将原始对象的数据复制到克隆体中之外,该方法有时还需处理克隆过程中的极端情况,例如克隆关联对象和梳理递归依赖等等。
客户端(Client)可以复制实现了原型接口的任何对象。
原型注册表实现
原型注册表(Prototype Registry)提供了一种访问常用原型的简单方法,其中存储了一系列可供随时复制的预生成对象。最简单的注册表原型是一个名称 → 原型
的哈希表。但如果需要使用名称以外的条件进行搜索,你可以创建更加完善的注册表版本。
原型模式适合应用场景
- 如果你需要复制一些对象,同时又希望代码独立于这些对象所属的具体类,可以使用原型模式。
这一点考量通常出现在代码需要处理第三方代码通过接口传递过来的对象时。即使不考虑代码耦合的情况,你的代码也不能依赖这些对象所属的具体类,因为你不知道它们的具体信息。
原型模式为客户端代码提供一个通用接口,客户端代码可通过这一接口与所有实现了克隆的对象进行交互,它也使得客户端代码与其所克隆的对象具体类独立开来。
- 如果子类的区别仅在于其对象的初始化方式,那么你可以使用该模式来减少子类的数量。别人创建这些子类的目的可能是为了创建特定类型的对象。
在原型模式中,你可以使用一系列预生成的、各种类型的对象作为原型。
客户端不必根据需求对子类进行实例化,只需找到合适的原型并对其进行克隆即可。
原型模式优缺点
优点:
- 你可以克隆对象,而无需与它们所属的具体类相耦合。
- 你可以克隆预生成原型,避免反复运行初始化代码。
- 你可以更方便地生成复杂对象。
- 你可以用继承以外的方式来处理复杂对象的不同配置。
缺点:
- 克隆包含循环引用的复杂对象可能会非常麻烦。
与其他模式的关系
-
在许多设计工作的初期都会使用工厂方法模式(较为简单,而且可以更方便地通过子类进行定制),随后演化为使用抽象工厂模式、原型模式或生成器模式(更灵活但更加复杂)。
-
原型并不基于继承,因此没有继承的缺点。另一方面,原型需要对被复制对象进行复杂的初始化。工厂方法基于继承,但是它不需要初始化步骤。
-
有时候原型可以作为备忘录模式的一个简化版本,其条件是你需要在历史记录中存储的对象的状态比较简单,不需要链接其他外部资源,或者链接可以方便地重建。
在 Java 中使用模式
使用示例: Java 的 Cloneable
(可克隆) 接口就是立即可用的原型模式。
任何类都可通过实现该接口来实现可被克隆的性质。
java.lang.Object#clone()
(类必须实现java.lang.Cloneable
接口)
示例代码
原型(Prototype)
public abstract class Shape {
public final int x;
public final int y;
public final String color;
public Shape(int x, int y, String color) {
this.x = x;
this.y = y;
this.color = color;
}
public Shape(Shape other) {
this.x = other.x;
this.y = other.y;
this.color = other.color;
}
public abstract Shape clone();
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Shape shape = (Shape) o;
return x == shape.x &&
y == shape.y &&
color.equals(shape.color);
}
@Override
public int hashCode() {
return Objects.hash(x, y, color);
}
}
具体原型 (Concrete Prototype)
public class Circle extends Shape {
public final int radius;
public Circle(int x, int y, String color, int radius) {
super(x, y, color);
this.radius = radius;
}
public Circle(Circle other) {
super(other);
this.radius = other.radius;
}
@Override
public Shape clone() {
return new Circle(this);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
Circle circle = (Circle) o;
return radius == circle.radius;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), radius);
}
}
public class Rectangle extends Shape {
public final int width;
public final int height;
public Rectangle(int width, int height, String color) {
super(0, 0, color);
this.width = width;
this.height = height;
}
public Rectangle(Rectangle other) {
super(other);
this.width = other.width;
this.height = other.height;
}
@Override
public Shape clone() {
return new Rectangle(this);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
Rectangle rectangle = (Rectangle) o;
return width == rectangle.width &&
height == rectangle.height;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), width, height);
}
}
客户端 (Client)
public class Client {
public static void main(String[] args) {
List<Shape> shapes = new ArrayList<>();
List<Shape> shapesCopy = new ArrayList<>();
Circle circle = new Circle(10, 20, "red", 15);
shapes.add(circle);
Circle anotherCircle = (Circle) circle.clone();
shapes.add(anotherCircle);
Rectangle rectangle = new Rectangle(10, 20, "blue");
shapes.add(rectangle);
for (Shape shape : shapes) {
shapesCopy.add(shape.clone());
}
for (int i = 0; i < shapes.size(); i++) {
if (shapes.get(i) != shapesCopy.get(i)) {
System.out.println(i + ": Shapes are different objects (yay!)");
if (shapes.get(i).equals(shapesCopy.get(i))) {
System.out.println(i + ": And they are identical (yay!)");
} else {
System.out.println(i + ": But they are not identical (booo!)");
}
} else {
System.out.println(i + ": Shape objects are the same (booo!)");
}
}
}
}