原型模式

来源:互联网 发布:法尔考实况巅峰数据 编辑:程序博客网 时间:2024/06/10 09:38

概念

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

意图

在运行期建立和删除原型。

适用性

  • 当一个系统应该独立于它的产品创建、构成和表示时,要使用Prototype模式;
  • 当要实例化的类是在运行时刻指定时,例如,通过动态装载;
  • 为了避免创建一个与产品类层次平行的工厂类层次时;
  • 当一个类的实例只能有几个不同状态组合中的一种时,建立相应数目的原型并克隆他们可能比每次用合适的状态手工实例化该类更方便一些。

参与者

Prototype:声明一个clone自身的接口。

ConcretePrototype:实现一个克隆自身的操作。

Client: 让一个原型克隆自身从而创建一个新的对象。

效果

1)运行时刻增加和删除产品。

2)改变值以指定新对象。

3)改变结构以指定新对象。

4)减少子类的构造。

5)用类动态配置应用。

6)每一个Prototype的子类都必须实现Clone操作,这可能很困难。

实现

我们将创建一个抽象类 Shape 和扩展了 Shape 类的实体类。下一步是定义类 ShapeCache,该类把 shape 对象存储在一个 Hashtable 中,并在请求的时候返回它们的克隆。

PrototypPatternDemo,我们的演示类使用 ShapeCache 类来获取 Shape 对象。

步骤 1

创建一个实现了 Clonable 接口的抽象类。

Shape.java

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;   }}

步骤 2

创建扩展了上面抽象类的实体类。

Rectangle.java

public class Rectangle extends Shape {   public Rectangle(){     type = "Rectangle";   }   @Override   public void draw() {      System.out.println("Inside Rectangle::draw() method.");   }}

Square.java

public class Square extends Shape {   public Square(){     type = "Square";   }   @Override   public void draw() {      System.out.println("Inside Square::draw() method.");   }}

Circle.java

public class Circle extends Shape {   public Circle(){     type = "Circle";   }   @Override   public void draw() {      System.out.println("Inside Circle::draw() method.");   }}

步骤 3

创建一个类,从数据库获取实体类,并把它们存储在一个 Hashtable 中。

ShapeCache.java

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);   }}


步骤 4

PrototypePatternDemo 使用 ShapeCache 类来获取存储在 Hashtable 中的形状的克隆。

PrototypePatternDemo.java

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());   }}


0 0
原创粉丝点击