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

