例如,一个对象需要在一个高代价的数据库操作之后被创建
我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用
原型模式属于创建型模式,它提供了一种创建对象的最佳方式
1. 意图
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
2. 主要解决
在运行期建立和删除原型
3. 何时使用
4. 如何解决
利用已有的一个原型对象,快速地生成和原型对象一样的实例
5. 关键代码
实现克隆操作
,在JAVA继承Cloneable
,重写clone(),在.NET中可以使用Object类的MemberwiseClone()
方法来实现对象的浅拷贝或通过序列化的方式来实现深拷贝;原型模式
同样用于隔离类对象的使用者和具体类型(易变类)
之间的耦合关系,它同样要求这些”易变类”拥有稳定的接口;6. 应用实例
7. 优点
8. 缺点
9. 使用场景
原型模式已经与 Java 融为浑然一体,大家可以随手拿来使用
10. 注意事项
Shape
和扩展了Shape
类的实体类;ShapeCache
,该类把shape对象存储在一个Hashtable
中,并在请求的时候返回它们的克隆;PrototypPatternDemo
类使用ShapeCache
类来获取Shape
对象;1. 创建一个实现了 Clonable 接口的抽象类 Shape.
Shape.java
package com.demo.gof;
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
package com.demo.gof;
public class Rectangle extends Shape {
public Rectangle(){
type = "Rectangle";
}
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
Square .java
package com.demo.gof;
public class Square extends Shape {
public Square(){
type = "Square";
}
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
Circle.java
package com.demo.gof;
public class Circle extends Shape{
public Circle(){
type = "Circle";
}
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
3. 创建类 ShapeCache,从数据库获取实体类,并把它们存储在一个 Hashtable 中
ShapeCache .java
package com.demo.gof;
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
package com.demo.gof;
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());
}
}
编译运行以上 Java 范例,输出结果如下
$ javac -d . src/main/com.demo/gof/PrototypePatternDemo.java
$ java com.demo.gof.PrototypePatternDemo
Shape : Circle
Shape : Square
Shape : Rectangle