【设计模式 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.
參考:
原型模式是通過復(fù)制已有對(duì)象來快速創(chuàng)建新對(duì)象的方法,它適用于創(chuàng)建那些實(shí)例化很慢的對(duì)象,比如數(shù)據(jù)庫連接對(duì)象,在創(chuàng)建好這樣的對(duì)象后,我們可以緩存一份,下次需要這種對(duì)象時(shí),我們可以直接返回一個(gè)該對(duì)象的拷貝。
使用場(chǎng)景
Java Cloneable接口
Java中提供了一個(gè)標(biāo)記接口Cloneable,類如果實(shí)現(xiàn)了這個(gè)接口就可以使用Object類中定義的clone方法
如果沒有實(shí)現(xiàn)Cloneable接口,直接調(diào)用clone()會(huì)拋出CloneNotSupportedException
Object clone()會(huì)返回當(dāng)前對(duì)象的一個(gè)淺拷貝
深拷貝和淺拷貝
根據(jù)不同的對(duì)象類型,拷貝的內(nèi)容也各不相同:
深拷貝 DeepCopy
Java中實(shí)現(xiàn)深拷貝可以手動(dòng)拷貝object類型的屬性,但如果這個(gè)類型中還有object類型,就會(huì)很麻煩。
@Override protected DCOut clone() throws CloneNotSupportedException {DCOut copy = (DCOut) super.clone();In copyIn = (In) this.in.clone();copy.setIn(copyIn);return copy; }還可以使用Serializable接口,通過序列化,將堆中的對(duì)象數(shù)據(jù)信息復(fù)制一份到堆外,再反序列化成新的克隆對(duì)象
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內(nèi)置的copy模塊提供了深拷貝和淺拷貝的功能,python中淺拷貝只會(huì)拷貝父對(duì)象,不會(huì)拷貝父對(duì)象內(nèi)部的子對(duì)象
python切片屬于淺拷貝
例
《大話設(shè)計(jì)模式》里簡(jiǎn)歷的例子
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 | 完整代碼
總結(jié)
以上是生活随笔為你收集整理的【设计模式 06】原型模式(克隆??)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第二次作业:软件分析之网易云音乐
- 下一篇: 科学计算机复杂公式计算公式,超级公式计算