欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java設(shè)計(jì)模式--原型模式詳解

 更新時(shí)間:2021年07月19日 15:32:52   作者:吾仄lo咚鏘  
這篇文章主要為大家詳細(xì)介紹了Java設(shè)計(jì)模式之Prototype原型模式的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

引例

問題:

現(xiàn)在有一只羊(包含屬性:名字Dolly、年齡2),需要克隆10只屬性完全相同的羊。

一般解法:

定義Sheep類表示羊,包括構(gòu)造器、getter()和toString()。

public class Sheep {
    private String name;
    private int age;
    public Sheep(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

在客戶端實(shí)例化多利,然后再根據(jù)多利的屬性去實(shí)例化10只羊。

public class Client {
    public static void main(String[] args) {
        Sheep sheepDolly=new Sheep("Dolly",2);
        Sheep sheep1 = new Sheep(sheepDolly.getName(), sheepDolly.getAge());
        Sheep sheep2 = new Sheep(sheepDolly.getName(), sheepDolly.getAge());
        Sheep sheep3 = new Sheep(sheepDolly.getName(), sheepDolly.getAge());
        //....
        System.out.println(sheep1+",hashCode:"+sheep1.hashCode());
        System.out.println(sheep2+",hashCode:"+sheep2.hashCode());
        System.out.println(sheep3+",hashCode:"+sheep3.hashCode());
        //...
    }
}

運(yùn)行結(jié)果

在這里插入圖片描述

優(yōu)缺點(diǎn):

這種方法是我們首先很容易就能想到的,也是絕大多數(shù)人的第一做法。

但缺點(diǎn)也很明顯,每次創(chuàng)建新對(duì)象時(shí)需要獲取原始對(duì)象的屬性,對(duì)象復(fù)雜時(shí)效率很低;此外不能動(dòng)態(tài)獲得對(duì)象運(yùn)行時(shí)的狀態(tài),若類增減屬性需要改動(dòng)代碼。

下面我們看下原型模式的解法。

原型模式

原型模式(Prototype Pattern)是一種創(chuàng)建型設(shè)計(jì)模式,允許一個(gè)對(duì)象再創(chuàng)建另外一個(gè)可定制的對(duì)象,無需知道如何創(chuàng)建的細(xì)節(jié)。即用原型實(shí)例指定創(chuàng)建對(duì)象的種類,并且通過拷貝這些原型,創(chuàng)建新的對(duì)象。

工作原理:將原型對(duì)象傳給那個(gè)要發(fā)動(dòng)創(chuàng)建的對(duì)象,這個(gè)要發(fā)動(dòng)創(chuàng)建的對(duì)象通過請(qǐng)求原型對(duì)象拷貝它們自己來實(shí)施創(chuàng)建。即用基類Object的clone()方法或序列化。

UML類圖:

在這里插入圖片描述

  • Prototype:原型類,聲明一個(gè)克隆自己的接口
  • ConcretePrototype: 具體的原型類, 實(shí)現(xiàn)一個(gè)克隆自己的操作
  • Client: 客戶端讓一個(gè)原型對(duì)象克隆自己,從而創(chuàng)建一個(gè)新的對(duì)象

原型模式又可分為淺拷貝和深拷貝,區(qū)別在于對(duì)引用數(shù)據(jù)類型的成員變量的拷貝,小朋友你是否有很多問號(hào)? 不急 ,看完這兩種方法實(shí)現(xiàn)你就懂了。

淺拷貝

在原先Sheep類基礎(chǔ)上實(shí)現(xiàn)Cloneable接口,重寫clone方法。

public class Sheep implements Cloneable{
    private String name;
    private int age;
    @Override
    protected Object clone()  {//克隆該實(shí)例,使用默認(rèn)的clone方法來完成
        Sheep sheep = null;
        try {
            sheep = (Sheep)super.clone();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sheep;
    }
    public Sheep(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

客戶端調(diào)用

public class Client {
    public static void main(String[] args) {
        Sheep sheepDolly=new Sheep("Dolly",2);
        Sheep sheep1 = (Sheep)sheepDolly.clone();
        Sheep sheep2 = (Sheep)sheepDolly.clone();
        Sheep sheep3 = (Sheep)sheepDolly.clone();
        //....
        System.out.println("sheep1:"+sheep1+",hashCode:" + sheep1.hashCode());
        System.out.println("sheep2:"+sheep2+",hashCode:" + sheep2.hashCode());
        System.out.println("sheep3:"+sheep3+",hashCode:" + sheep3.hashCode());
        //...
    }
}

運(yùn)行結(jié)果

在這里插入圖片描述

至此,原型模式的淺拷貝也成功克隆了三個(gè)對(duì)象,但是看進(jìn)度條發(fā)現(xiàn)并不簡單。

現(xiàn)在小羊有了一個(gè)朋友小牛,Sheep類添加了一個(gè)引用屬性Cow,我們同樣再克隆一遍。

Sheep類

public class Sheep implements Cloneable{
    private String name;
    private int age;
    public Cow friend;//新朋友Cow對(duì)象,其余不變
    @Override
    protected Object clone()  {
        Sheep sheep = null;
        try {
            sheep = (Sheep)super.clone();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sheep;
    }
    public Sheep(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

新添的Cow類

public class Cow {
    private String name;
    private int age;
    public Cow(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Cow{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

客戶端調(diào)用克隆

public class Client {
    public static void main(String[] args) {
        Sheep sheepDolly=new Sheep("Dolly",2);
        sheepDolly.friend=new Cow("Tom",1); //并實(shí)例化朋友
        Sheep sheep1 = (Sheep)sheepDolly.clone();
        Sheep sheep2 = (Sheep)sheepDolly.clone();
        Sheep sheep3 = (Sheep)sheepDolly.clone();
        //....
        System.out.println("sheep1:"+sheep1+",hashCode:" + sheep1.hashCode());
        System.out.println("sheep1.friend:"+sheep1.friend+",hashCode:" + sheep1.friend.hashCode()+'\n');
        System.out.println("sheep2:"+sheep2+",hashCode:" + sheep2.hashCode());
        System.out.println("sheep2.friend:"+sheep2.friend+",hashCode:" + sheep2.friend.hashCode()+'\n');
        System.out.println("sheep3:"+sheep3+",hashCode:" + sheep3.hashCode());
        System.out.println("sheep3.friend:"+sheep3.friend+",hashCode:" + sheep3.friend.hashCode()+'\n');
        //...
    }
}

運(yùn)行結(jié)果

在這里插入圖片描述

通過運(yùn)行結(jié)果發(fā)現(xiàn),淺拷貝通過Object的clone()成功克隆實(shí)例化了三個(gè)新對(duì)象,但是并沒有克隆實(shí)例化對(duì)象中的引用屬性,也就是沒有克隆friend對(duì)象(禁止套娃 ),三個(gè)新克隆對(duì)象的friend還是指向原克隆前的friend,即同一個(gè)對(duì)象。

這樣的話,他們四個(gè)的friend是引用同一個(gè),若一個(gè)對(duì)象修改了friend屬性,勢(shì)必會(huì)影響其他三個(gè)對(duì)象的該成員變量值。

小結(jié):

  • 淺拷貝是使用默認(rèn)的 clone()方法來實(shí)現(xiàn)
  • 基本數(shù)據(jù)類型的成員變量,淺拷貝會(huì)直接進(jìn)行值傳遞(復(fù)制屬性值給新對(duì)象)。
  • 引用數(shù)據(jù)類型的成員變量,淺拷貝會(huì)進(jìn)行引用傳遞(復(fù)制引用值(內(nèi)存地址)給新對(duì)象)。

深拷貝

方法一:

機(jī)靈的人兒看出,再clone一遍cow不就好了,但是手動(dòng)遞歸下去不推薦。

1.Cow類也實(shí)現(xiàn)Cloneable接口

public class Cow implements Cloneable{
    private String name;
    private int age;
    public Cow(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //無引用類型,直接clone即可
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone(); //直接拋出了,沒用try-catch
    }
    @Override
    public String toString() {
        return "Cow{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Sheep類的clone再添加調(diào)用cow的clone

public class Sheep implements Cloneable{
    private String name;
    private int age;
    public Cow friend;//新朋友Cow對(duì)象,其余不變
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Object deep = null;
        //完成對(duì)基本數(shù)據(jù)類型(屬性)和String的克隆
        deep = super.clone();
        //對(duì)引用類型的屬性,進(jìn)行再次clone
        Sheep sheep = (Sheep)deep;
        sheep.friend  = (Cow)friend.clone();
        return sheep;
    }
    public Sheep(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

客戶端調(diào)用

public class Client {
    public static void main(String[] args) throws CloneNotSupportedException {
        Sheep sheepDolly=new Sheep("Dolly",2);
        sheepDolly.friend=new Cow("Tom",1); //并實(shí)例化朋友
        Sheep sheep1 = (Sheep)sheepDolly.clone();
        Sheep sheep2 = (Sheep)sheepDolly.clone();
        Sheep sheep3 = (Sheep)sheepDolly.clone();
        //....
        System.out.println("sheep1:"+sheep1+",hashCode:" + sheep1.hashCode());
        System.out.println("sheep1.friend:"+sheep1.friend+",hashCode:" + sheep1.friend.hashCode()+'\n');
        System.out.println("sheep2:"+sheep2+",hashCode:" + sheep2.hashCode());
        System.out.println("sheep2.friend:"+sheep2.friend+",hashCode:" + sheep2.friend.hashCode()+'\n');
        System.out.println("sheep3:"+sheep3+",hashCode:" + sheep3.hashCode());
        System.out.println("sheep3.friend:"+sheep3.friend+",hashCode:" + sheep3.friend.hashCode()+'\n');
        //...
    }
}

運(yùn)行結(jié)果

在這里插入圖片描述

方法二:

通過對(duì)象序列化實(shí)現(xiàn)深拷貝(推薦)

1.Cow類實(shí)現(xiàn)序列化接口,不必實(shí)現(xiàn)Cloneable接口了

public class Cow implements Serializable {
    private String name;
    private int age;
    public Cow(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Cow{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2.在Sheep類實(shí)現(xiàn)序列化接口

public class Sheep implements Serializable { //實(shí)現(xiàn)序列化接口
    private String name;
    private int age;
    public Cow friend;

    public Sheep(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    public Object deepClone() { //深拷貝
        //創(chuàng)建流對(duì)象
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            //序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this); //當(dāng)前這個(gè)對(duì)象以對(duì)象流的方式輸出
            //反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            Sheep sheep = (Sheep) ois.readObject();
            return sheep;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            //關(guān)閉流
            try {
                bos.close();
                oos.close();
                bis.close();
                ois.close();
            } catch (Exception e2) {
                System.out.println(e2.getMessage());
            }
        }
    }
}

3.客戶端調(diào)用

public class Client {
    public static void main(String[] args) throws CloneNotSupportedException {
        Sheep sheepDolly=new Sheep("Dolly",2);
        sheepDolly.friend=new Cow("Tom",1); //并實(shí)例化朋友
        Sheep sheep1 = (Sheep)sheepDolly.deepClone();
        Sheep sheep2 = (Sheep)sheepDolly.deepClone();
        Sheep sheep3 = (Sheep)sheepDolly.deepClone();
        //....
        System.out.println("sheep1:"+sheep1+",hashCode:" + sheep1.hashCode());
        System.out.println("sheep1.friend:"+sheep1.friend+",hashCode:" + sheep1.friend.hashCode()+'\n');
        System.out.println("sheep2:"+sheep2+",hashCode:" + sheep2.hashCode());
        System.out.println("sheep2.friend:"+sheep2.friend+",hashCode:" + sheep2.friend.hashCode()+'\n');
        System.out.println("sheep3:"+sheep3+",hashCode:" + sheep3.hashCode());
        System.out.println("sheep3.friend:"+sheep3.friend+",hashCode:" + sheep3.friend.hashCode()+'\n');
        //...
    }
}

運(yùn)行結(jié)果

在這里插入圖片描述

原型模式總結(jié):

  • 創(chuàng)建新的對(duì)象比較復(fù)雜時(shí),可以利用原型模式簡化對(duì)象的創(chuàng)建過程,同時(shí)也能夠提高效率
  • 可以不用重新初始化對(duì)象,動(dòng)態(tài)地獲得對(duì)象運(yùn)行時(shí)的狀態(tài)。
  • 如果原始對(duì)象發(fā)生變化(增加或者減少屬性),其它克隆對(duì)象的也會(huì)發(fā)生相應(yīng)的變化,無需修改代碼
  • 若成員變量無引用類型,淺拷貝clone即可;若引用類型的成員變量很少,可考慮遞歸實(shí)現(xiàn)clone,否則推薦序列化。

總結(jié)

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • jdbc和mybatis的流式查詢使用方法

    jdbc和mybatis的流式查詢使用方法

    有些時(shí)候我們所需要查詢的數(shù)據(jù)量比較大,但是jvm內(nèi)存又是有限制的,數(shù)據(jù)量過大會(huì)導(dǎo)致內(nèi)存溢出。這個(gè)時(shí)候就可以使用流式查詢,本文就主要介紹了jdbc和mybatis的流式查詢,感興趣的可以了解一下
    2021-11-11
  • SpringBoot中Mockito單元測試入門

    SpringBoot中Mockito單元測試入門

    單元測試在很多地方都用的到,本文主要介紹了SpringBoot中Mockito單元測試入門,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • java分頁攔截類實(shí)現(xiàn)sql自動(dòng)分頁

    java分頁攔截類實(shí)現(xiàn)sql自動(dòng)分頁

    這篇文章主要為大家詳細(xì)介紹了java分頁攔截類可以實(shí)現(xiàn)sql自動(dòng)分頁,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Java通過調(diào)用C/C++實(shí)現(xiàn)的DLL動(dòng)態(tài)庫——JNI的方法

    Java通過調(diào)用C/C++實(shí)現(xiàn)的DLL動(dòng)態(tài)庫——JNI的方法

    這篇文章主要介紹了Java通過調(diào)用C/C++實(shí)現(xiàn)的DLL動(dòng)態(tài)庫——JNI的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • MyBatis在mapper中傳遞參數(shù)的四種方式

    MyBatis在mapper中傳遞參數(shù)的四種方式

    MyBatis是一個(gè)持久層框架,它提供了一種將數(shù)據(jù)庫操作與Java對(duì)象之間的映射關(guān)系進(jìn)行配置的方式,在MyBatis中,Mapper是用于定義數(shù)據(jù)庫操作的接口,而參數(shù)傳遞則是通過Mapper接口的方法來實(shí)現(xiàn)的,本文給大家介紹了MyBatis在mapper中傳遞參數(shù)的四種方式,需要的朋友可以參考下
    2024-03-03
  • java-SSH2實(shí)現(xiàn)數(shù)據(jù)庫和界面的分頁

    java-SSH2實(shí)現(xiàn)數(shù)據(jù)庫和界面的分頁

    本文主要是介紹SSH2實(shí)現(xiàn)數(shù)據(jù)庫和界面的分頁的代碼,分頁在web應(yīng)用中是經(jīng)常要做的事情,實(shí)用性比較大,有需要的朋友可以來了解一下。
    2016-10-10
  • java實(shí)現(xiàn)百度云OCR文字識(shí)別 高精度OCR識(shí)別身份證信息

    java實(shí)現(xiàn)百度云OCR文字識(shí)別 高精度OCR識(shí)別身份證信息

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)百度云OCR文字識(shí)別,高精度OCR識(shí)別身份證信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • IDEA 每次新建工程都要重新配置 Maven的解決方案

    IDEA 每次新建工程都要重新配置 Maven的解決方案

    這篇文章主要介紹了IDEA 每次新建工程都要重新配置Maven解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Java基于正則表達(dá)式實(shí)現(xiàn)查找匹配的文本功能【經(jīng)典實(shí)例】

    Java基于正則表達(dá)式實(shí)現(xiàn)查找匹配的文本功能【經(jīng)典實(shí)例】

    這篇文章主要介紹了Java基于正則表達(dá)式實(shí)現(xiàn)查找匹配的文本功能,結(jié)合具體實(shí)例形式分析了java正則查找、字符串遍歷、group分組相關(guān)操作技巧,需要的朋友可以參考下
    2017-04-04
  • Java集合List的使用詳細(xì)解析

    Java集合List的使用詳細(xì)解析

    這篇文章主要介紹了Java集合List的使用詳細(xì)解析,List集合類中元素有序、且可重復(fù),集合中的每個(gè)元素都有其對(duì)應(yīng)的順序索引,鑒于Java中數(shù)組用來存儲(chǔ)數(shù)據(jù)的局限性,我們通常使用java.util.List替代數(shù)組,需要的朋友可以參考下
    2023-11-11

最新評(píng)論