Java設計模式之Prototype原型模式
一、場景描述
創(chuàng)建型模式中,從工廠方法模式,抽象工廠模式,到建造者模式,再到原型模式,我的理解是,創(chuàng)建對象的方式逐步從編碼實現(xiàn)轉向內存對象處理。
例如,在“儀器數(shù)據(jù)采集器”的子類/對象“PDF文件數(shù)據(jù)采集器”和“Excel文件數(shù)據(jù)采集器”的創(chuàng)建過程中
工廠模式下定義各子類,并由(抽象)工廠類Factory創(chuàng)建,因此各子類可在類定義中定義各自的屬性;
建造者模式下,通過不同的創(chuàng)建者類Builder創(chuàng)建不同的子對象,此時不再定義子類;
而原型模式下,則完全由調用者基于父對象克隆創(chuàng)建子對象,不在針對子對象創(chuàng)建類或者其相關的工廠、建造者類。
三種模式對應于不同的場景,實際操作時,根據(jù)場景合理選擇模式。
原型模式下,基于原型類對象,克隆創(chuàng)建新對象,因此為原型類對象賦予的屬性值在新對象中可直接使用,免去了重復賦值;
例如儀器數(shù)據(jù)采集器的共同初始化工作可在原型類對象中完成,隨后將其克隆出PDF文件數(shù)據(jù)采集器對象和Excel文件數(shù)據(jù)采集器對象,并為兩對象屬性做后續(xù)的擴展,免去了公共屬性的初始化工作;
克隆操作在內存中完成,由于對象類型的屬性值存儲為引用,因此克隆分淺克隆和深克隆,通過Serializable接口實現(xiàn)深克隆。
二、示例代碼
原型類:
package lims.designpatterndemo.prototypedemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class EquipmentDataCapture implements Cloneable, Serializable { private String filePath = "file path"; private String equipmentData = "file content"; // public String getFilePath() { return this.filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getEquipmentData() { return this.equipmentData; } public void setEquipmentData(String equipmentData) { this.equipmentData = equipmentData; } // private static final long serialVersionUID = 1L; private SerializableObject obj; // public SerializableObject getObj() { return obj; } public void setObj(SerializableObject obj) { this.obj = obj; } // public EquipmentDataCapture getEquipmentDataCapture() throws CloneNotSupportedException { EquipmentDataCapture capture = (EquipmentDataCapture) super.clone(); return capture; } // public EquipmentDataCapture getPdfFileCapture() throws CloneNotSupportedException { // EquipmentDataCapture capture = (EquipmentDataCapture) super.clone(); // capture.setEquipmentData("pdf file content"); // return capture; // } // public EquipmentDataCapture getExcelFileCapture() throws CloneNotSupportedException { // EquipmentDataCapture capture = (EquipmentDataCapture) super.clone(); // capture.setEquipmentData("excel file content"); // return capture; // } /* 深復制 */ public EquipmentDataCapture newEquipmentDataCapture() throws IOException, ClassNotFoundException { /* 寫入當前對象的二進制流 */ ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); /* 讀出二進制流產生的新對象 */ ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); return (EquipmentDataCapture)ois.readObject(); } } class SerializableObject implements Serializable { private static final long serialVersionUID = 1L; }
調用端:
package lims.designpatterndemo.prototypedemo; public class PrototypeDemo { public static void main(String[] args) throws CloneNotSupportedException { EquipmentDataCapture edc = new EquipmentDataCapture(); EquipmentDataCapture capture = null; // capture = edc.getPdfFileCapture(); // capture = edc.getExcelFileCapture(); capture = edc.getEquipmentDataCapture(); capture.setEquipmentData("equipment data file content"); String fileContent = capture.getEquipmentData(); System.out.println(fileContent); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java實現(xiàn)跳躍表(skiplist)的簡單實例
這篇文章主要介紹了Java編程中跳躍表的概念和實現(xiàn)原理,并簡要敘述了它的結構,具有一定參考價值,需要的朋友可以了解下。2017-09-09java:程序包org.springframework.boot不存在的完美解決方法
最近項目中運行的時候提示了"java: 程序包org.springframework.boot不存在",下面這篇文章主要給大家介紹了關于java:程序包org.springframework.boot不存在的完美解決方法,需要的朋友可以參考下2023-05-05解決Process.getInputStream()阻塞的問題
這篇文章主要介紹了解決Process.getInputStream()阻塞的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot發(fā)送異步郵件流程與實現(xiàn)詳解
這篇文章主要介紹了SpringBoot發(fā)送異步郵件流程與實現(xiàn)詳解,Servlet階段郵件發(fā)送非常的復雜,如果現(xiàn)代化的Java開發(fā)是那個樣子該有多糟糕,現(xiàn)在SpringBoot中集成好了郵件發(fā)送的東西,而且操作十分簡單容易上手,需要的朋友可以參考下2024-01-01