设计模式:原型模式

意图

原型模式(Prototype)是一种创建型设计模式使你能够复制已有对象而又无需使代码依赖它们所属的类

解决方案

原型模式将克隆过程委派给被克隆的实际对象模式为所有支持克隆的对象声明了一个通用接口该接口让你能够克隆对象同时又无需将代码和对象所属类耦合通常情况下这样的接口中仅包含一个 克隆方法

所有的类对 克隆方法的实现都非常相似该方法会创建一个当前类的对象然后将原始对象所有的成员变量值复制到新建的类中你甚至可以复制私有成员变量因为绝大部分编程语言都允许对象访问其同类对象的私有成员变量

支持克隆的对象即为原型当你的对象有几十个成员变量和几百种类型时对其进行克隆甚至可以代替子类的构造

其运作方式如下:创建一系列不同类型的对象并用不同的方式对其进行配置。如果所需对象与预先配置的对象相同,那么你只需克隆原型即可,无需新建一个对象。

原型模式结构

基本实现

原型设计模式的结构

原型Prototype接口将对克隆方法进行声明在绝大多数情况下其中只会有一个名为 clone克隆的方法

具体原型Concrete Prototype)类将实现克隆方法。除了将原始对象的数据复制到克隆体中之外,该方法有时还需处理克隆过程中的极端情况,例如克隆关联对象和梳理递归依赖等等。

客户端Client)可以复制实现了原型接口的任何对象。

原型注册表实现

原型注册表

原型注册表Prototype Registry提供了一种访问常用原型的简单方法其中存储了一系列可供随时复制的预生成对象最简单的注册表原型是一个名称 → 原型的哈希表但如果需要使用名称以外的条件进行搜索你可以创建更加完善的注册表版本

原型模式适合应用场景

  • 如果你需要复制一些对象同时又希望代码独立于这些对象所属的具体类可以使用原型模式

这一点考量通常出现在代码需要处理第三方代码通过接口传递过来的对象时即使不考虑代码耦合的情况你的代码也不能依赖这些对象所属的具体类因为你不知道它们的具体信息

原型模式为客户端代码提供一个通用接口客户端代码可通过这一接口与所有实现了克隆的对象进行交互它也使得客户端代码与其所克隆的对象具体类独立开来

  • 如果子类的区别仅在于其对象的初始化方式那么你可以使用该模式来减少子类的数量别人创建这些子类的目的可能是为了创建特定类型的对象

在原型模式中你可以使用一系列预生成的各种类型的对象作为原型

客户端不必根据需求对子类进行实例化只需找到合适的原型并对其进行克隆即可

原型模式优缺点

优点:

  • 你可以克隆对象而无需与它们所属的具体类相耦合
  •  你可以克隆预生成原型避免反复运行初始化代码
  •  你可以更方便地生成复杂对象
  •  你可以用继承以外的方式来处理复杂对象的不同配置

缺点:

  • 克隆包含循环引用的复杂对象可能会非常麻烦。

与其他模式的关系

在 Java 中使用模式

使用示例 Java 的 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!)");
      }
    }
  }
}