第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java如何在保持兼容性的同時擴展第3方應用程序

Java如何在保持兼容性的同時擴展第3方應用程序

不負相思意 2021-09-12 16:24:36
我正在開發(fā)基于開源項目的應用程序。我們付錢給開發(fā)人員,讓他們根據(jù)他們的代碼制作我們自己的封閉源代碼應用程序。我們確實希望與原始軟件包保持兼容,安全性和功能更新將與我們的應用程序合并。當然,如果我大量修改原始代碼,這里最大的問題是與新更新的合并。我在網(wǎng)上搜索了最佳的解決方案和他人之間,我發(fā)現(xiàn)喜歡的網(wǎng)站這一個。所以我決定盡可能不修改原始代碼。如果我想向現(xiàn)有特性添加功能,我會擴展原始類,使應用程序指向我的擴展類(這是一個足夠小的修改,不會成為問題),然后實現(xiàn)我的更改。這里的問題是第 3 方類中的屬性屬于我也想擴展的類型。在下面的示例class A中,對myClass我希望它繼續(xù)執(zhí)行的屬性進行了一些修改。但是class B,其 extends class A,包含一個具有擴展MyClass自身類型的屬性,這里是MyCustomClass。在內(nèi)部,class B我希望該屬性myCustomClass是 的擴展myClass,對所做的任何更改都myClass應該可以在myCustomClass.據(jù)我所知,不可能強制myClass轉(zhuǎn)換myCustomClass為,因為myClass根本不是 type myCustomClass。那么這里最好的解決方案是保持兼容性class A并且不涉及僅僅復制所有代碼,class A因為如果class A獲得更新會產(chǎn)生完全相同的問題?這里有一些示例代碼可以更好地解釋我的問題:// Classes 'A' and 'MyClass' are 3rd party, I do not want to edit thempublic class A {    private MyClass myClass;    A(){        myClass = new MyClass();        // Changes I want to keep doing        doSomething();    }    public void doSomething(){        // Do some calculations to myClass and modify it accordingly     }    public MyClass getMyClass(){        return myClass    }}// Classes 'B' and 'MyCustomClass' are 1st party, I can do anything I want with them :)public class B extends A{    private MyCustomClass myCustomClass;            B(){        super();        myCustomClass = ?????????;        // Extremely important message, this should keep working        System.out.println(myCustomClass.verySpecialString);        // Build further on made modifications, this should keep working too        useModificationsByClassA();    }    public void useModificationsByClassA(){        // Use result of class A's calculations in order to continue    }}public class MyCustomClass extends MyClass{    private String verySpecialString;    MyCustomClass(){        super();        verySpecialString = "Hey!";    }}
查看完整描述

2 回答

?
RISEBY

TA貢獻1856條經(jīng)驗 獲得超5個贊

考慮使用適配器或裝飾器模式,并使用組合而不是繼承。例如:


public class B {

    private final A a;        

    private final MyCustomClass myCustomClass;


    B(A a) {

        this.a = a;

        myCustomerClass = new MyCustomClass(a.getMyClass());

    }


   public void doSomething()  {

     a.doSomething();   // updates the state of a.myClass.

     myCustomerClass.doSomething();  // use the change of a.myClass to do more stuff         

   }

}


public class MyCustomClass {

   private final MyClass myClass;

   private String verySpecialString;


   public MyCustomClass(MyClass myClass) {

      this.myClass = myClass;

   }


   public void doSomething() {

      verySpecialString = "Hey!" + myClass.getSomeResult();

   }

}


查看完整回答
反對 回復 2021-09-12
?
一只名叫tom的貓

TA貢獻1906條經(jīng)驗 獲得超3個贊

一般而言,在應用程序、庫等 for... 中,某些組件被設計為可擴展(因為雖然是 for),而某些其他組件實際上并未被設計為可擴展。
但是修改一個現(xiàn)有的組件通常不是擴展應用程序行為的方式。
因為現(xiàn)有的組件通常不是為了修改而設計的。他們的實際邏輯/處理可能會從一個版本到另一個版本發(fā)生很大變化。這些可能會從源代碼或其他任何東西中刪除......因此您將很難將原始源的更新合并到應用程序修改后的實現(xiàn)的源代碼中。

長話短說:應用程序可能允許擴展,但在這種情況下,它也提供了擴展點,您不能以任何方式更改任何類。
如果您想擴展原始源代碼的某些部分,您必須知道實際應用程序是否旨在擴展,如果是,如何擴展
如果它不是為擴展而設計的,您應該接受應用程序更新可能不會集成(或難以集成)到修改后的應用程序中。


查看完整回答
反對 回復 2021-09-12
  • 2 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號