1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        不調(diào)用構(gòu)造函數(shù),如何創(chuàng)建對(duì)象?

        共 3402字,需瀏覽 7分鐘

         ·

        2021-09-22 10:33

        設(shè)計(jì)模式專欄

         


        祝大家中秋節(jié)快樂(lè),至此中秋佳節(jié),可樂(lè)繼續(xù)為大家肝設(shè)計(jì)模式。在介紹這個(gè)設(shè)計(jì)模式時(shí),先拋出一個(gè)問(wèn)題:

        如何不調(diào)用構(gòu)造函數(shù),創(chuàng)建對(duì)象?

        1、什么是原型模式

        Specify the kinds of objects to create using a prototypical instance,and create new objects by copying this prototype.

        Prototype Design Pattern:用原型實(shí)例指定創(chuàng)建對(duì)象的種類, 并且通過(guò)拷貝這些原型創(chuàng)建新的對(duì)象。

        說(shuō)人話:對(duì)象復(fù)制

        2、原型模式的兩種實(shí)現(xiàn)方法

        我們?nèi)粘i_(kāi)發(fā)中,應(yīng)該有使用過(guò) BeanUtils.copyProperties()方法,其實(shí)這就是原型模式的一種用法(淺拷貝)。原型模式實(shí)現(xiàn)分兩種:

        ①、淺拷貝:只會(huì)復(fù)制對(duì)象中基本數(shù)據(jù)類型數(shù)據(jù)和引用對(duì)象的內(nèi)存地址,不會(huì)遞歸地復(fù)制引用對(duì)象,以及引用對(duì)象的引用對(duì)象

        ②、深拷貝:得到的是一份完完全全獨(dú)立的對(duì)象。

        Java 中 Object 類是所有類的根類,Object 類提供了一個(gè) clone()方法,該方法可以將一個(gè) Java 對(duì)象復(fù)制一份,但是在調(diào)用 clone方法的Java類必須要實(shí)現(xiàn)一個(gè)接口Cloneable,這是一個(gè)標(biāo)志接口,標(biāo)志該類能夠復(fù)制且具有復(fù)制的能力,如果不實(shí)現(xiàn) Cloneable 接口,直接調(diào)用clone方法,會(huì)拋出 CloneNotSupportedException 異常。

        /**
         * A class implements the <code>Cloneable</code> interface to
         * indicate to the {@link java.lang.Object#clone()} method that it
         * is legal for that method to make a
         * field-for-field copy of instances of that class.
         * <p>
         * Invoking Object's clone method on an instance that does not implement the
         * <code>Cloneable</code> interface results in the exception
         * <code>CloneNotSupportedException</code> being thrown.
         * <p>
         * By convention, classes that implement this interface should override
         * <tt>Object.clone</tt> (which is protected) with a public method.
         * See {@link java.lang.Object#clone()} for details on overriding this
         * method.
         * <p>
         * Note that this interface does <i>not</i> contain the <tt>clone</tt> method.
         * Therefore, it is not possible to clone an object merely by virtue of the
         * fact that it implements this interface.  Even if the clone method is invoked
         * reflectively, there is no guarantee that it will succeed.
         *
         * @author  unascribed
         * @see     java.lang.CloneNotSupportedException
         * @see     java.lang.Object#clone()
         * @since   JDK1.0
         */

        public interface Cloneable {
        }

        關(guān)于深淺拷貝的詳細(xì)說(shuō)明,可以參考我的這篇博客:

        https://www.cnblogs.com/ysocean/p/8482979.html

        3、原型模式的優(yōu)點(diǎn)

        ①、性能高

        原型模式是在內(nèi)存二進(jìn)制流的拷貝, 要比直接new一個(gè)對(duì)象性能好很多, 特別是要在一個(gè)循環(huán)體內(nèi)產(chǎn)生大量的對(duì)象時(shí), 原型模式可以更好地體現(xiàn)其優(yōu)點(diǎn)。

        ②、避免構(gòu)造函數(shù)的約束

        這既是它的優(yōu)點(diǎn)也是缺點(diǎn),直接在內(nèi)存中拷貝,構(gòu)造函數(shù)是不會(huì)執(zhí)行的 。優(yōu)點(diǎn)就是減少了約束, 缺點(diǎn)也是減少了約束, 需要大家在實(shí)際應(yīng)用時(shí)考慮。

        這里也給出了文章標(biāo)題的答案。

        4、原型模式使用場(chǎng)景

        ①、在需要一個(gè)類的大量對(duì)象的時(shí)候,使用原型模式是最佳選擇,因?yàn)樵湍J绞窃趦?nèi)存中對(duì)這個(gè)對(duì)象進(jìn)行拷貝,要比直接new這個(gè)對(duì)象性能要好很多,在這種情況下,需要的對(duì)象越多,原型模式體現(xiàn)出的優(yōu)點(diǎn)越明顯。

        ②、如果一個(gè)對(duì)象的初始化需要很多其他對(duì)象的數(shù)據(jù)準(zhǔn)備或其他資源的繁瑣計(jì)算,那么可以使用原型模式。

        ③、當(dāng)需要一個(gè)對(duì)象的大量公共信息,少量字段進(jìn)行個(gè)性化設(shè)置的時(shí)候,也可以使用原型模式拷貝出現(xiàn)有對(duì)象的副本進(jìn)行加工處理。

        關(guān)于我

        可樂(lè)是一個(gè)熱愛(ài)技術(shù)的Java程序猿,公眾號(hào)「IT可樂(lè)」定期分享有趣有料的精品原創(chuàng)文章!

        非常感謝各位人才能看到這里,原創(chuàng)不易,文章如果有幫助可以關(guān)注、點(diǎn)贊、分享或評(píng)論,這都是對(duì)我的莫大支持!

        愿你我人生盡量沒(méi)有遺憾的事,愿你我都能奔赴在各自想去的路上。


        瀏覽 133
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            手机看色片| 欧美操逼软件 | 欧美老妇人禽猛交视频 | 国外黄色小视频 | 国产精品偷窥熟女视频大全 | 17c白丝喷水 自慰 | 一级黄色视频在线观看 | 五月丁香开心婷婷 | 少妇激情偷人三级 | 亚洲性爱电影院 |