《java編程思想 泛型》混入

混入的概念

把多個(gè)類的能力混合在一起產(chǎn)生一個(gè)能代表所有混入類的類型的類。
the fundamental concept is that of mixing in capabilities from multiple classes in order to produce a resulting class that represents all the types of the mixins.
混入就是把存在的多個(gè)類映射為一個(gè)新的子類。
a mixin as a function that maps existing classes to new subclasses.

混入的價(jià)值

被混入的類如果發(fā)生改變則改變會(huì)影響到所有混入該類的類。
混入跟AOP很類似,同時(shí)也建議使用AOP來(lái)解決混入的問(wèn)題。

C++混入

C++的多繼承可以實(shí)現(xiàn)混入。
參數(shù)類型實(shí)現(xiàn)混入是最好的方式,一個(gè)類繼承類型參數(shù)。

使用接口實(shí)現(xiàn)混入


組合優(yōu)于配置,雖然JDK8之前的接口是不能有

使用裝飾模式實(shí)現(xiàn)混入——裝飾器的目的

滿足各種可能的組合

使用裝飾模式實(shí)現(xiàn)混入——裝飾器的做法

The Decorator pattern uses layered objects to dynamically and transparently
add responsibilities to individual objects.

具有相同的接口。

Decorators are implemented using composition and formal structures (the
decoratable/decorator hierarchy), whereas mixins are inheritance-based.
參數(shù)類型的混入可以被當(dāng)做是不需要繼承結(jié)構(gòu)的裝飾器模式。

使用裝飾器實(shí)現(xiàn)混入只有最后一層的方法是可見(jiàn)的。

使用動(dòng)態(tài)代理實(shí)現(xiàn)混入

動(dòng)態(tài)代理實(shí)現(xiàn)混入的最大好處就是生成的類是所有被混入類的子類。
動(dòng)態(tài)代理實(shí)現(xiàn)混入的最大缺點(diǎn)就是每個(gè)混入的類必須實(shí)現(xiàn)接口。

使用之前必須先進(jìn)行強(qiáng)制類型轉(zhuǎn)換。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容