Java设计模式——原型模式(Prototype Pattern)
来源:互联网 发布:中美抗苏 知乎 编辑:程序博客网 时间:2024/06/03 09:14
原型模式
原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。
原型模式的定义
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
原型模式结构详解
原型模式主要用于对象的复制,它的核心是就是类图中的原型类Prototype。Prototype类需要具备以下两个条件:
实现Cloneable接口。在java语言有一个Cloneable接口,它的作用只有一个,就是在运行时通知虚拟机可以安全地在实现了此接口的类上使用clone方法。在java虚拟机中,只有实现了这个接口的类才可以被拷贝,否则在运行时会抛出CloneNotSupportedException异常。
重写Object类中的clone方法。Java中,所有类的父类都是Object类,Object类中有一个clone方法,作用是返回对象的一个拷贝,但是其作用域protected类型的,一般的类无法调用,因此,Prototype类需要将clone方法的作用域修改为public类型。
原型模式的机构图
原型模式代码实例
创建一个抽象类 Shape 和扩展了 Shape 类的实体类。下一步是定义类 ShapeCache,该类把 shape 对象存储在一个 Hashtable 中,并在请求的时候返回它们的克隆。
PrototypPatternDemo,我们的演示类使用 ShapeCache 类来获取 Shape 对象。
第一步:创建一个实现了 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; }}
第二步:创建扩展了上面抽象类的实体类。
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."); }}
第三步:创建一个类,从数据库获取实体类,并把它们存储在一个 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); }}
第四步: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()); }}
第五步:验证输出
Shape : CircleShape : SquareShape : Rectangle
原型模式的优缺点
优点
1、性能提高。
2、逃避构造函数的约束。
缺点
1、配备克隆方法需要对类的功能进行通盘考虑,这对于全新的类不是很难,但对于已有的类不一定很容易,特别当一个类引用不支持串行化的间接对象,或者引用含有循环结构的时候。
2、必须实现 Cloneable 接口。
3、逃避构造函数的约束。
原型模式适用场景
1、资源优化场景。
2、类初始化需要消化非常多的资源,这个资源包括数据、硬件资源等。
3、性能和安全要求的场景。
4、通过 new 产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式。
5、一个对象多个修改者的场景。
6、一个对象需要提供给其他对象访问,而且各个调用者可能都需要修改其值时,可以考虑使用原型模式拷贝多个对象供调用者使用。
7、在实际项目中,原型模式很少单独出现,一般是和工厂方法模式一起出现,通过 clone 的方法创建一个对象,然后由工厂方法提供给调用者。
使用原型模式需要注意
与通过对一个类进行实例化来构造新对象不同的是,原型模式是通过拷贝一个现有对象生成新对象的。浅拷贝实现 Cloneable,重写,深拷贝是通过实现 Serializable 读取二进制流。
- Java设计模式——原型模式(Prototype Pattern)
- java设计模式——原型模式(Prototype Pattern)
- Java设计模式——原型模式(Prototype Pattern)
- 设计模式(6)——原型模式(Prototype Pattern)
- Java设计模式--原型模式【Prototype Pattern】
- 设计模式-原型模式(Prototype Pattern)
- 【设计模式】原型模式(Prototype Pattern)
- JAVA设计模式 — 原型模式(Prototype)
- 设计模式之——Prototype Pattern(原型模式)
- 设计模式——原型模式(Prototype Pattern)
- 设计模式——创建型模式之原型模式(Prototype Pattern)
- java设计模式——原型模式(Prototype)
- JAVA设计模式——原型模式(Prototype)
- java设计模式5——原型模式(Prototype)
- Java 设计模式——原型模式(Prototype)
- Java Prototype Pattern(原型模式)
- 设计模式:创建型模式:原型模式(prototype pattern)
- java设计模式---Prototype Pattern---原型模式(复制建立对象)
- 输入10个整数,用直接插入法对其进行排序;
- 容灾设计学习笔记
- 三年小记
- 三层
- hibernate 实体关联
- Java设计模式——原型模式(Prototype Pattern)
- 6. ZigZag Conversion
- npm初入门
- IDE创建JavaWeb项目
- 【Linux学习之路】——在虚拟机VirtualBox上安装CentOS 7.x
- PHP将8字节ASCII码数据表示转化成浮点数值
- 将项目打成war包
- Ctrip App 启动流程
- 2017-2018-1 20155332 《信息安全系统设计基础》第3周学习总结