溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java中的原始模型模式是什么

發(fā)布時間:2021-08-13 10:18:17 來源:億速云 閱讀:165 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“java中的原始模型模式是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“java中的原始模型模式是什么”吧!

目錄
  • 什么是原始模型模式

  • 原始模型模式中的角色

  • 抽象原型角色(Prototype)

  • 具體原型角色(ConcretePrototype)?????

  • 客戶端角色(Client)

  • 使用Java內(nèi)置機(jī)制實現(xiàn)原始模型模式

  • 淺拷貝和深拷貝

  • 怎么實現(xiàn)深拷貝


什么是原始模型模式

通過給出一個原型對象指明所要創(chuàng)建的對象的類型,然后通過復(fù)制這個原型對象來獲取的更多的同類型的對象。

這讓我不由自主的想起克隆技術(shù),還記得克隆羊嗎?我們接下來講的內(nèi)容和克隆羊不能說關(guān)系密切,只能說毫無關(guān)系。

java中的原始模型模式是什么

設(shè)計模式和編程語言無關(guān),但是二當(dāng)家的依然用Java語言去實戰(zhàn)舉例。而且Java有標(biāo)準(zhǔn)的實現(xiàn)原始模型模式的方法。

原始模型模式中的角色

java中的原始模型模式是什么

Prototype:抽象類或者一個接口,給出具體模型需要的接口。ConcretePrototype:繼承抽象原型模型角色,被復(fù)制的對象。Client:提出復(fù)制請求。

抽象原型角色(Prototype)

我們用家庭作業(yè)為抽象原型角色(Prototype)。我們這里的作業(yè)是可以抄的。大家不要學(xué)哈。

package com.secondgod.prototype;
/**
 * 作業(yè)
 *
 * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
 */
public interface IHomework {
    /**
     * 抄一份
     * @return
     */
    IHomework copy();
    /**
     * 修改所有者
     * @param owner
     */
    void setOwner(String owner);
}

具體原型角色(ConcretePrototype)?????

我們用語文作業(yè)作為具體原型角色(ConcretePrototype)。

package com.secondgod.prototype;
import java.text.MessageFormat;
/**
 * 語文作業(yè)
 *
 * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
 */
public class ChineseHomework implements IHomework {
    /**
     * 作業(yè)的所有者
     */
    private String owner;
    /**
     * 作業(yè)標(biāo)題/作業(yè)要求
     */
    private String title;
    /**
     * 作業(yè)內(nèi)容
     */
    private String content;
    public ChineseHomework(String owner, String title, String content) {
        this.owner = owner;
        this.title = title;
        this.content = content;
    }
    public String getOwner() {
        return owner;
    }
    public void setOwner(String owner) {
        this.owner = owner;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String toString() {
        return MessageFormat.format("owner:{0},title:{1},content:{2}", owner, title, content);
    }
    @Override
    public IHomework copy() {
        ChineseHomework homework = new ChineseHomework(this.getOwner(), this.getTitle(), this.getContent());
        return homework;
    }
}

客戶端角色(Client)

我們測試一下。

package com.secondgod.prototype;
/**
 * 測試原始模型
 *
 * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Test {
    public static void main(String[] args) {
        // 老師讓寫作業(yè),大當(dāng)家按時完成
        IHomework homework = new ChineseHomework("大當(dāng)家的", "作文-最崇拜的人", "不瞞你們說,我最崇拜的是二當(dāng)家的");
        // 二當(dāng)家的沒按時完成,決定去抄大當(dāng)家的作業(yè)~
        IHomework newHomework = homework.copy();
        newHomework.setOwner("二當(dāng)家的");
        System.out.println(homework);
        System.out.println(newHomework);
    }
}

java中的原始模型模式是什么

和我們的預(yù)期一致

使用Java內(nèi)置機(jī)制實現(xiàn)原始模型模式

在Object類中有這樣一個方法,Java中所有的類都繼承自O(shè)bject類,也就是說所有的類內(nèi)部都可以復(fù)制自己。但是卻不能直接調(diào)用別的類的克隆方法。也就是說有統(tǒng)一的方式,但是默認(rèn)不可用。

java中的原始模型模式是什么

我們改一下語文作業(yè)類,使用clone方法去嘗試,克隆自己。

package com.secondgod.prototype;
import java.text.MessageFormat;
/**
 * 語文作業(yè)
 *
 * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
 */
public class ChineseHomework implements IHomework {
    /**
     * 作業(yè)的所有者
     */
    private String owner;
    /**
     * 作業(yè)標(biāo)題/作業(yè)要求
     */
    private String title;
    /**
     * 作業(yè)內(nèi)容
     */
    private String content;
    public ChineseHomework(String owner, String title, String content) {
        this.owner = owner;
        this.title = title;
        this.content = content;
    }
    public String getOwner() {
        return owner;
    }
    public void setOwner(String owner) {
        this.owner = owner;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String toString() {
        return MessageFormat.format("owner:{0},title:{1},content:{2}", owner, title, content);
    }
    @Override
    public IHomework copy() {
        try {
            return (ChineseHomework) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }
}

java中的原始模型模式是什么

這時候會報錯,因為我們還少做一件事。我們需要把語文作業(yè)類實現(xiàn)Cloneable接口,然而這個接口里沒有任何抽象方法,僅僅是一個標(biāo)記接口。這就像注解一樣,就是為了明確聲明可以克隆。

java中的原始模型模式是什么

實現(xiàn)接口后,則可以正確運行。

package com.secondgod.prototype;
import java.text.MessageFormat;
/**
 * 語文作業(yè)
 *
 * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
 */
public class ChineseHomework implements IHomework, Cloneable {
    /**
     * 作業(yè)的所有者
     */
    private String owner;
    /**
     * 作業(yè)標(biāo)題/作業(yè)要求
     */
    private String title;
    /**
     * 作業(yè)內(nèi)容
     */
    private String content;
    public ChineseHomework(String owner, String title, String content) {
        this.owner = owner;
        this.title = title;
        this.content = content;
    }
    public String getOwner() {
        return owner;
    }
    public void setOwner(String owner) {
        this.owner = owner;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String toString() {
        return MessageFormat.format("owner:{0},title:{1},content:{2}", owner, title, content);
    }
    @Override
    public IHomework copy() {
        try {
            return (ChineseHomework) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }
}

java中的原始模型模式是什么

和我們自己實現(xiàn)copy效果一樣。clone是一個native方法,是交給本地實現(xiàn)的,通常是直接內(nèi)存拷貝。

淺拷貝和深拷貝

淺拷貝:創(chuàng)建一個新對象,然后將當(dāng)前對象的非靜態(tài)字段復(fù)制到該新對象,如果字段是值類型的,那么對該字段執(zhí)行復(fù)制;如果該字段是引用類型的話,則復(fù)制引用但不復(fù)制引用的對象。因此,原始對象及其副本引用同一個對象。

深拷貝:創(chuàng)建一個新對象,然后將當(dāng)前對象的非靜態(tài)字段復(fù)制到該新對象,無論該字段是值類型的還是引用類型,都復(fù)制獨立的一份。當(dāng)你修改其中一個對象的任何內(nèi)容時,都不會影響另一個對象的內(nèi)容。

二當(dāng)家的理解方式是,淺拷貝就是僅拷貝當(dāng)前對象的內(nèi)容,深拷貝就是遞歸拷貝當(dāng)前對象和當(dāng)前對象的引用類型屬性的內(nèi)容,直到全部都是基本數(shù)據(jù)類型的屬性為止。另外,僅僅使用clone方法是淺拷貝。

package com.secondgod.prototype;/** * 測試原始模型 * * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/ */public class Test implements Cloneable {    private Field field;    public Field getField() {        return field;    }    public void setField(Field field) {        this.field = field;    }    public Test clone() throws CloneNotSupportedException {        return (Test) super.clone();    }    public static void main(String[] args) throws CloneNotSupportedException {        Test t = new Test();        t.setField(new Field());        Test cloneT = t.clone();        System.out.println(t == cloneT);        System.out.println(t.getField() == cloneT.getField());    }}class Field {}

java中的原始模型模式是什么

源對象和克隆出的新對象field屬性值是同一個對象。所以是淺拷貝。

怎么實現(xiàn)深拷貝

讓每個引用類型屬性內(nèi)部都重寫clone() 方法,然后需要對所有引用類型屬性都調(diào)用clone方法。

package com.secondgod.prototype;
/**
 * 測試原始模型
 *
 * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Test implements Cloneable {
    private Field field;
    public Field getField() {
        return field;
    }
    public void setField(Field field) {
        this.field = field;
    }
    public Test clone() throws CloneNotSupportedException {
        return (Test) super.clone();
    }
    public static void main(String[] args) throws CloneNotSupportedException {
        Test t = new Test();
        t.setField(new Field());
        Test cloneT = t.clone();
        System.out.println(t == cloneT);
        System.out.println(t.getField() == cloneT.getField());
    }
}
class Field {
}

java中的原始模型模式是什么

這種方式需要遞歸每個引用類型的屬性,要把他們的類都實現(xiàn)深拷貝,只到僅有基本數(shù)據(jù)類型為止。

利用序列化,這是個偷懶的辦法。但是二當(dāng)家的覺得更安全,如果你確定是要深拷貝,使用該方式可以防止某處漏實現(xiàn)clone方法,而變成不完全的深拷貝。

package com.secondgod.prototype;
/**
 * 測試原始模型
 *
 * @author 二當(dāng)家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Test implements Cloneable {
    private Field field;
    public Field getField() {
        return field;
    }
    public void setField(Field field) {
        this.field = field;
    }
    public Test clone() throws CloneNotSupportedException {
        return (Test) super.clone();
    }
    public Test deepClone() throws CloneNotSupportedException {
        Test t = new Test();
        t.setField(this.getField().deepClone());
        return t;
    }
    public static void main(String[] args) throws CloneNotSupportedException {
        Test t = new Test();
        t.setField(new Field());
        Test cloneT = t.clone();
        System.out.println(t == cloneT);
        System.out.println(t.getField() == cloneT.getField());
        Test deepCloneT = t.deepClone();
        System.out.println(t == deepCloneT);
        System.out.println(t.getField() == deepCloneT.getField());
    }
}
class Field implements Cloneable {
    public Field clone() throws CloneNotSupportedException {
        return (Field) super.clone();
    }
    public Field deepClone() throws CloneNotSupportedException {
        // 沒有引用類型屬性
        return this.clone();
    }
}

java中的原始模型模式是什么

到底使用淺拷貝還是深拷貝,要根據(jù)實際情況。但是有一點是確定的,深拷貝需要創(chuàng)建更多對象,占用更多內(nèi)存。

到此,相信大家對“java中的原始模型模式是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI