【设计模式 06】原型模式(克隆??)
原型模式(clone?)
Prototype pattern refers to creating duplicate object while keeping performance in mind. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
參考:
原型模式是通過復制已有對象來快速創建新對象的方法,它適用于創建那些實例化很慢的對象,比如數據庫連接對象,在創建好這樣的對象后,我們可以緩存一份,下次需要這種對象時,我們可以直接返回一個該對象的拷貝。
使用場景
Java Cloneable接口
Java中提供了一個標記接口Cloneable,類如果實現了這個接口就可以使用Object類中定義的clone方法
如果沒有實現Cloneable接口,直接調用clone()會拋出CloneNotSupportedException
Object clone()會返回當前對象的一個淺拷貝
深拷貝和淺拷貝
根據不同的對象類型,拷貝的內容也各不相同:
深拷貝 DeepCopy
Java中實現深拷貝可以手動拷貝object類型的屬性,但如果這個類型中還有object類型,就會很麻煩。
@Override protected DCOut clone() throws CloneNotSupportedException {DCOut copy = (DCOut) super.clone();In copyIn = (In) this.in.clone();copy.setIn(copyIn);return copy; }還可以使用Serializable接口,通過序列化,將堆中的對象數據信息復制一份到堆外,再反序列化成新的克隆對象
import java.io.*;public class DeepClone implements Serializable {private Object obj;public DeepClone(Object obj){this.obj = obj;}public Object deepClone() {Object result = null;//序列化ByteArrayOutputStream baos = new ByteArrayOutputStream();ObjectOutputStream oos = null;try {oos = new ObjectOutputStream(baos);oos.writeObject(obj);} catch (IOException e) {e.printStackTrace();}// 反序列化ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());ObjectInputStream ois = null;try {ois = new ObjectInputStream(bais);result = ois.readObject();} catch (IOException | ClassNotFoundException e) {e.printStackTrace();}return result;} }python中的深拷貝和淺拷貝
In [1]: import copyIn [2]: a = [i for i in range(10)]In [3]: b = copy.copy(a)In [4]: a is b Out[4]: FalseIn [5]: c = [[1, 2], [3, 4]]In [6]: d = copy.copy(c)In [7]: d is c Out[7]: FalseIn [8]: d[0] is c[0] Out[8]: TrueIn [9]: e = copy.deepcopy(c)In [10]: e[0] is c[0] Out[10]: FalseIn [11]:python內置的copy模塊提供了深拷貝和淺拷貝的功能,python中淺拷貝只會拷貝父對象,不會拷貝父對象內部的子對象
python切片屬于淺拷貝
例
《大話設計模式》里簡歷的例子
package pers.junebao.prototype_pattern;import pers.junebao.prototype_pattern.deep_copy.DeepClone;import java.io.Serializable;public class Resume implements Cloneable, Serializable {private String name;private String education;private String sex;Resume(String name) {this.name = name;}public void setName(String name) {this.name = name;}public void setEducation(String education) {this.education = education;}public void setSex(String sex) {this.sex = sex;}public void print(){System.out.println("name: " + this.name);System.out.println("sex : " + this.sex);System.out.println("education: " + this.education);}@Overridepublic Resume clone() {Resume resume = null;// 深拷貝resume = (Resume) DeepClone.deepClone(this);return resume;} } package pers.junebao.prototype_pattern;public class Main {public static void main(String[] args) {Resume resume = new Resume("JuneBao");resume.setSex("男");resume.setEducation("本科");Resume resume1 = resume.clone();resume1.setSex("女");resume.print();resume1.print();} }GitHub | 完整代碼
總結
以上是生活随笔為你收集整理的【设计模式 06】原型模式(克隆??)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第二次作业:软件分析之网易云音乐
- 下一篇: 科学计算机复杂公式计算公式,超级公式计算