Java設(shè)計(jì)模式之Prototype原型模式
一、場(chǎng)景描述
創(chuàng)建型模式中,從工廠方法模式,抽象工廠模式,到建造者模式,再到原型模式,我的理解是,創(chuàng)建對(duì)象的方式逐步從編碼實(shí)現(xiàn)轉(zhuǎn)向內(nèi)存對(duì)象處理。
例如,在“儀器數(shù)據(jù)采集器”的子類(lèi)/對(duì)象“PDF文件數(shù)據(jù)采集器”和“Excel文件數(shù)據(jù)采集器”的創(chuàng)建過(guò)程中
工廠模式下定義各子類(lèi),并由(抽象)工廠類(lèi)Factory創(chuàng)建,因此各子類(lèi)可在類(lèi)定義中定義各自的屬性;
建造者模式下,通過(guò)不同的創(chuàng)建者類(lèi)Builder創(chuàng)建不同的子對(duì)象,此時(shí)不再定義子類(lèi);
而原型模式下,則完全由調(diào)用者基于父對(duì)象克隆創(chuàng)建子對(duì)象,不在針對(duì)子對(duì)象創(chuàng)建類(lèi)或者其相關(guān)的工廠、建造者類(lèi)。
三種模式對(duì)應(yīng)于不同的場(chǎng)景,實(shí)際操作時(shí),根據(jù)場(chǎng)景合理選擇模式。
原型模式下,基于原型類(lèi)對(duì)象,克隆創(chuàng)建新對(duì)象,因此為原型類(lèi)對(duì)象賦予的屬性值在新對(duì)象中可直接使用,免去了重復(fù)賦值;
例如儀器數(shù)據(jù)采集器的共同初始化工作可在原型類(lèi)對(duì)象中完成,隨后將其克隆出PDF文件數(shù)據(jù)采集器對(duì)象和Excel文件數(shù)據(jù)采集器對(duì)象,并為兩對(duì)象屬性做后續(xù)的擴(kuò)展,免去了公共屬性的初始化工作;
克隆操作在內(nèi)存中完成,由于對(duì)象類(lèi)型的屬性值存儲(chǔ)為引用,因此克隆分淺克隆和深克隆,通過(guò)Serializable接口實(shí)現(xiàn)深克隆。
二、示例代碼
原型類(lèi):
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; // } /* 深復(fù)制 */ public EquipmentDataCapture newEquipmentDataCapture() throws IOException, ClassNotFoundException { /* 寫(xiě)入當(dāng)前對(duì)象的二進(jìn)制流 */ ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); /* 讀出二進(jìn)制流產(chǎn)生的新對(duì)象 */ 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; }
調(diào)用端:
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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)跳躍表(skiplist)的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Java編程中跳躍表的概念和實(shí)現(xiàn)原理,并簡(jiǎn)要敘述了它的結(jié)構(gòu),具有一定參考價(jià)值,需要的朋友可以了解下。2017-09-09java:程序包org.springframework.boot不存在的完美解決方法
最近項(xiàng)目中運(yùn)行的時(shí)候提示了"java: 程序包org.springframework.boot不存在",下面這篇文章主要給大家介紹了關(guān)于java:程序包org.springframework.boot不存在的完美解決方法,需要的朋友可以參考下2023-05-05解決Process.getInputStream()阻塞的問(wèn)題
這篇文章主要介紹了解決Process.getInputStream()阻塞的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06MyBatis?Plus?導(dǎo)入IdType失敗的解決
這篇文章主要介紹了MyBatis?Plus?導(dǎo)入IdType失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot發(fā)送異步郵件流程與實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringBoot發(fā)送異步郵件流程與實(shí)現(xiàn)詳解,Servlet階段郵件發(fā)送非常的復(fù)雜,如果現(xiàn)代化的Java開(kāi)發(fā)是那個(gè)樣子該有多糟糕,現(xiàn)在SpringBoot中集成好了郵件發(fā)送的東西,而且操作十分簡(jiǎn)單容易上手,需要的朋友可以參考下2024-01-01Java前后端的JSON傳輸方式(前后端JSON格式轉(zhuǎn)換)
這篇文章主要介紹了Java前后端的JSON傳輸方式(前后端JSON格式轉(zhuǎn)換),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04IDEA下從零開(kāi)始搭建SpringBoot工程的方法步驟
這篇文章主要介紹了IDEA下從零開(kāi)始搭建SpringBoot工程的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01淺談java字符串比較到底應(yīng)該用==還是equals
這篇文章主要介紹了淺談java字符串比較到底應(yīng)該用==還是equals,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12