Fork me on GitHub

设计模式(6)

创建型模式-原型模式

介绍

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

实例

  • 创建一个实现了Clonable接口的抽象类
    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
    public abstract class Shape implements Cloneable {

    private String id;
    protected String type;

    abstract void draw();

    public String getType(){
    return type;
    }

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public Object clone() {
    Object clone = null;
    try {
    clone = super.clone();
    } catch (CloneNotSupportedException e) {
    e.printStackTrace();
    }
    return clone;
    }
    }
  • 创建扩展了上面抽象类的实体类。

    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
    public class Rectangle extends Shape {

    public Rectangle(){
    type = "Rectangle";
    }

    @Override
    public void draw() {
    System.out.println("Inside Rectangle::draw() method.");
    }
    }

    public class Square extends Shape {

    public Square(){
    type = "Square";
    }

    @Override
    public void draw() {
    System.out.println("Inside Square::draw() method.");
    }
    }

    public class Circle extends Shape {

    public Circle(){
    type = "Circle";
    }

    @Override
    public void draw() {
    System.out.println("Inside Circle::draw() method.");
    }
    }
  • 创建一个类,从数据库获取实体类,并把它们存储在一个Hashtable中。

    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
    import java.util.Hashtable;

    public class ShapeCache {

    private static Hashtable<String, Shape> shapeMap
    = new Hashtable<String, Shape>();

    public static Shape getShape(String shapeId) {
    Shape cachedShape = shapeMap.get(shapeId);
    return (Shape) cachedShape.clone();
    }

    // 对每种形状都运行数据库查询,并创建该形状
    // shapeMap.put(shapeKey, shape);
    // 例如,我们要添加三种形状
    public static void loadCache() {
    Circle circle = new Circle();
    circle.setId("1");
    shapeMap.put(circle.getId(),circle);

    Square square = new Square();
    square.setId("2");
    shapeMap.put(square.getId(),square);

    Rectangle rectangle = new Rectangle();
    rectangle.setId("3");
    shapeMap.put(rectangle.getId(),rectangle);
    }
    }
  • PrototypePatternDemo使用ShapeCache类来获取存储在 Hashtable中的形状的克隆。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public class PrototypePatternDemo {
    public static void main(String[] args) {
    ShapeCache.loadCache();

    Shape clonedShape = (Shape) ShapeCache.getShape("1");
    System.out.println("Shape : " + clonedShape.getType());

    Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
    System.out.println("Shape : " + clonedShape2.getType());

    Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
    System.out.println("Shape : " + clonedShape3.getType());
    }
    }
  • 输出结果

    1
    2
    3
    Shape : Circle
    Shape : Square
    Shape : Rectangle

总结

这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。

参考

菜鸟教程