创造性模式——原型模式
原型模式(Prototype Pattern)是用于創建重復的對象,同時又能保證性能。這種類型的設計模式屬于創建型模式,它提供了一種創建對象的最佳方式。
這種模式是實現了一個原型接口,該接口用于創建當前對象的克隆。當直接創建對象的代價比較大時,則采用這種模式。例如,一個對象需要在一個高代價的數據庫操作之后被創建。我們可以緩存該對象,在下一個請求時返回它的克隆,在需要的時候更新數據庫,以此來減少數據庫調用
一、介紹
概述:用原型實例指定創建對象的種類,并且通過拷貝這些原型創建新的對象。
適用場景:
實現方式:利用已有的一個原型對象,快速地生成和原型對象一樣的實例。
優點:
缺點:
使用場景范例:
PS:與通過對一個類進行實例化來構造新對象不同的是,原型模式是通過拷貝一個現有對象生成新對象的。淺拷貝實現 Cloneable,重寫,深拷貝是通過實現 Serializable 讀取二進制流。
二、范例
我們將創建一個抽象類 Shape 和擴展了 Shape 類的實體類。下一步是定義類 ShapeCache,該類把 shape 對象存儲在一個 Hashtable 中,并在請求的時候返回它們的克隆。
PrototypePatternDemo 類使用 ShapeCache 類來獲取 Shape 對象。
步驟 1
創建一個實現了 Cloneable 接口的抽象類。
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";}@Overridepublic void draw() {System.out.println("Inside Rectangle::draw() method.");} }Square.java
public class Square extends Shape {public Square() {type = "Square";}@Overridepublic void draw() {System.out.println("Inside Square::draw() method.");} }Circle.java
public class Circle extends Shape {public Circle() {type = "Circle";}@Overridepublic 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());} }步驟 5
執行程序,輸出結果:
Shape : Circle
Shape : Square
Shape : Rectangle
總結
以上是生活随笔為你收集整理的创造性模式——原型模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JDK源码解析之 Java.lang.C
- 下一篇: java中的构造方法与代码块