JAVA設計模式(三)——原型模式
阿新 • • 發佈:2020-09-14
原型模式也屬於建立型模式,該模式的思想就是將一個物件作為原型,對其進行復制、克隆,產生一個和原物件類似的新物件。其中包含了深複製和淺複製兩種複製。
淺複製:將一個物件複製後,基本資料型別的變數都會重新建立,而引用型別,指向的還是原物件所指向的。
深複製:將一個物件複製後,不論是基本資料型別還有引用型別,都是重新建立的。
深複製的實現方式有2種,
①單獨對其中物件再次克隆
②實現序列化,通過流的方式實現
簡單來說,就是深複製進行了完全徹底的複製,而淺複製不徹底。
模式型別 | 優點 | 缺點 | 應用場景 |
原型模式 |
1、效能提高。 2、逃避建構函式的約束。 |
1.必須實現 Cloneable 介面 2.深複製需要進序列化 |
1.一個物件多個修改者的場景 2.效能和安全要求的場景 3.資源優化場景 |
建立Prototype類
package com.designpatterns.create.prototype; import java.io.*; public class Prototype implements Cloneable, Serializable { private static final long serialVersionUID = 6700381124074207478L; private String string;View Codeprivate SerializableObject obj; /* 淺複製 */ public Object clone() throws CloneNotSupportedException { Prototype proto = (Prototype) super.clone(); return proto; } /* 深複製 */ public Object deepClone() 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 ois.readObject(); } public String getString() { return string; } public void setString(String string) { this.string = string; } public SerializableObject getObj() { return obj; } public void setObj(SerializableObject obj) { this.obj = obj; } @Override public String toString() { final StringBuffer sb = new StringBuffer("Prototype{"); sb.append("string='").append(string).append('\''); sb.append(", obj=").append(obj); sb.append('}'); return sb.toString(); } }
建立SerializableObject類
package com.designpatterns.create.prototype; import java.io.Serializable; public class SerializableObject implements Serializable { private static final long serialVersionUID = 5415290762824927634L; private Integer age; private String name; private String address; public SerializableObject(Integer age, String name, String address) { this.age = age; this.name = name; this.address = address; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { final StringBuffer sb = new StringBuffer("SerializableObject{"); sb.append("age=").append(age); sb.append(", name='").append(name).append('\''); sb.append(", address='").append(address).append('\''); sb.append('}'); return sb.toString(); } }View Code
測試
/** * *原型模式:將一個物件作為原型,對其進行復制、克隆,產生一個和原物件類似的新物件 * 淺複製: 只賦值基礎型別,引用型別和原物件的致 * 深複製: 完全賦值,引用型別也是全新的物件 * @author wyj * @date 2020/8/7 14:25 */ public class PrototypeTest { public static void main(String[] args) throws Exception { Prototype prototype = new Prototype(); prototype.setString("麼麼噠"); SerializableObject serializableObject = new SerializableObject(100, "趙波藍", "四川"); prototype.setObj(serializableObject); /*淺複製*/ Prototype o1 = (Prototype)prototype.clone(); System.out.println(serializableObject.equals(o1.getObj())); /*深複製*/ Prototype o = (Prototype)prototype.deepClone(); System.out.println(serializableObject.equals(o.getObj())); } }