Java設(shè)計(jì)模式之橋接模式實(shí)例詳解
本文實(shí)例講述了Java設(shè)計(jì)模式之橋接模式。分享給大家供大家參考,具體如下:
概念:
橋接模式(Bridge Pattern):將抽象部分與它的實(shí)現(xiàn)部分分離,使它們都可以獨(dú)立地變化。
橋接模式將繼承關(guān)系轉(zhuǎn)換為關(guān)聯(lián)關(guān)系,從而降低了類與類之間的耦合,減少了代碼編寫量。
什么情況下會(huì)用橋接模式?
簡(jiǎn)單的說(shuō)就是我們?cè)诔橄髮?duì)象的特征時(shí),對(duì)象的特征屬性又很抽象,不得不把屬性再次抽象。
否則的話,具體子類的數(shù)量將會(huì)成幾何增長(zhǎng),而且不易擴(kuò)展。沒(méi)辦法維護(hù)現(xiàn)有代碼。
舉例,我們?cè)诔橄笫謾C(jī)這二個(gè)對(duì)象時(shí),它的幾個(gè)屬性,如操作系統(tǒng),cpu,屏幕,運(yùn)營(yíng)商網(wǎng)絡(luò)等都很復(fù)雜。我們不能簡(jiǎn)單的把這幾個(gè)屬性直接定義,必須再次抽象化。而具體的一個(gè)手機(jī)對(duì)象就是這些屬性的組合,但不是簡(jiǎn)單的組合,屬性需要實(shí)現(xiàn)自己作為屬性的功能。在這樣的設(shè)計(jì)下,代碼的維護(hù)和擴(kuò)展也就容易了。
注意:在說(shuō)這個(gè)模式的時(shí)候,我不能保證說(shuō)的和寫得例子都是正確的,畢竟我也是新接觸到,所有例子均基于與個(gè)人理解。
我認(rèn)為的橋接模式說(shuō)明圖:
下面是例子:
1. 首先定義抽象類,抽象和描述對(duì)象的特征。
在對(duì)象的屬性上劃分維度,為了以后橋接和擴(kuò)展。
package test.design.bridge; public abstract class CellPhone { private String cellPhoneName; public CellPhoneSystem cellPhoneSystem; public CellPhoneCPU cellPhoneCPU; public void works(){ System.out.println("---------------------"); System.out.println("This cellphone is:"+this.getCellPhoneName()+",welcome to use. "); System.out.println("This cellphone detail infomation:"); System.out.println("系統(tǒng)類型:"+this.getCellPhoneSystem().getSystemName()); System.out.println("cpu型號(hào):"+this.getCellPhoneCPU().getCpuName()); System.out.println("---------------------"); } public String getCellPhoneName() { return cellPhoneName; } public void setCellPhoneName(String cellPhoneName) { this.cellPhoneName = cellPhoneName; } public CellPhoneSystem getCellPhoneSystem() { return cellPhoneSystem; } public void setCellPhoneSystem(CellPhoneSystem cellPhoneSystem) { this.cellPhoneSystem = cellPhoneSystem; } public CellPhoneCPU getCellPhoneCPU() { return cellPhoneCPU; } public void setCellPhoneCPU(CellPhoneCPU cellPhoneCPU) { this.cellPhoneCPU = cellPhoneCPU; } }
2. 屬性維度的抽象。(可以使用接口定義,關(guān)鍵看你的具體功能)
package test.design.bridge; /** * 屬性cpu被抽象成一個(gè)維度,為了以后擴(kuò)展 * @author lushuaiyin * */ public abstract class CellPhoneCPU { public CellPhone cellPhone; public String cpuName; public void cpuWorks(){ System.out.println("I am cpu. My pattern is:"+this.getCpuName()); System.out.println("I am working for this cellphone:"+this.getCellPhone().getCellPhoneName()); } public CellPhone getCellPhone() { return cellPhone; } public void setCellPhone(CellPhone cellPhone) { this.cellPhone = cellPhone; this.getCellPhone().setCellPhoneCPU(this);// 裝配(橋接,或者可以認(rèn)為對(duì)象類與其屬性類的傳遞) } public String getCpuName() { return cpuName; } public void setCpuName(String cpuName) { this.cpuName = cpuName; } }
package test.design.bridge; /** * 屬性操作系統(tǒng)被抽象成一個(gè)維度,為了以后擴(kuò)展 * @author lushuaiyin * */ public abstract class CellPhoneSystem { public CellPhone cellPhone; public String SystemName; public void systemWorks(){ System.out.println("I am "+this.getSystemName()+" system."); System.out.println("I am working for this cellphone:"+this.getCellPhone().getCellPhoneName()); } public CellPhone getCellPhone() { return cellPhone; } public void setCellPhone(CellPhone cellPhone) { this.cellPhone = cellPhone; this.getCellPhone().setCellPhoneSystem(this);// 裝配(橋接,或者可以認(rèn)為對(duì)象類與其屬性類的傳遞) } public String getSystemName() { return SystemName; } public void setSystemName(String systemName) { SystemName = systemName; } }
3. 具體的維度屬性對(duì)象。
這里我們?cè)诓僮飨到y(tǒng)屬性和cpu屬性上各定義2個(gè)具體對(duì)象,
package test.design.bridge; public class AndroidSystem extends CellPhoneSystem{ }
package test.design.bridge; public class IOSSystem extends CellPhoneSystem{ }
package test.design.bridge; /** * 雙核cpu * @author Administrator * */ public class TwoCore extends CellPhoneCPU{ }
package test.design.bridge; /** * 四核cpu * @author Administrator * */ public class FourCore extends CellPhoneCPU{ }
4. 測(cè)試代碼。
其中說(shuō)了在需要擴(kuò)展維度的情況下,怎么擴(kuò)展的。
定義一個(gè)手機(jī)對(duì)象
package test.design.bridge; public class Phone1 extends CellPhone{ //具體對(duì)象的屬性與邏輯 }
測(cè)試main函數(shù)
package test.design.bridge; public class TestMain { /** * @param args */ public static void main(String[] args) { //任何一種具體的對(duì)象都是復(fù)雜多種屬性的集合,在此可以看出橋接模式在構(gòu)建對(duì)象時(shí)的靈活性 //產(chǎn)生一個(gè)具體對(duì)象1 CellPhone p1=new Phone1(); p1.setCellPhoneName(" IPhone 6 "); CellPhoneSystem system1=new IOSSystem();//操作系統(tǒng)屬性維度 system1.setSystemName("ios7"); system1.setCellPhone(p1);//裝配 system1.systemWorks();//工作 /*裝配說(shuō)的簡(jiǎn)單點(diǎn)就是傳值。因?yàn)槲覀儼岩粋€(gè)對(duì)象的屬性按維度分開(kāi)來(lái)了, 那么橋接的時(shí)候就必須相互傳遞對(duì)象。即對(duì)象類可以調(diào)用子屬相類對(duì)象, 子屬性類對(duì)象也可以調(diào)用該對(duì)象類. 關(guān)于這樣的傳值方式有多種,你可以在構(gòu)造函數(shù)中傳遞,也可以在 調(diào)用具體邏輯方法時(shí)傳遞。這里我直接用set方法傳遞,只是為了更清楚. 如果某個(gè)屬性維度是必須出現(xiàn)的,那就可以在抽象類的構(gòu)造函數(shù)中傳入*/ CellPhoneCPU cpu1=new TwoCore();//cpu屬性維度 cpu1.setCpuName("A6"); cpu1.setCellPhone(p1); cpu1.cpuWorks(); p1.works();//最終整體對(duì)象功能 /* 橋接模式就是為了應(yīng)對(duì)屬性的擴(kuò)展,在此說(shuō)的屬性必須是在維度確定的情況下。 比如,這里我們?cè)诙x手機(jī)對(duì)象時(shí),確定兩個(gè)屬性維度:操作系統(tǒng)和cpu型號(hào)。 以后再這兩個(gè)屬性中,需要擴(kuò)展時(shí),就可以使用該模式。比如,一種新的cpu 型號(hào)出現(xiàn)了,那么我不用重新設(shè)計(jì)現(xiàn)在的代碼,只要增添一個(gè)cpu類即可。 如果出現(xiàn)了新的維度屬性,比如手機(jī)對(duì)象必須考慮屏幕大小。那橋接模式 在此就需要從根本上修改代碼來(lái)了。 */ System.out.println("-----------分割---------------------------"); //在cpu維度上擴(kuò)展。比如出現(xiàn)新型cpu:8核三星Exynos 5 Octa芯片". //三星手機(jī)推出了GALAXY Note Ⅲ就是使用這種新型cpu. 寫一個(gè)新類EightCore擴(kuò)展cpu維度. //同時(shí)定義這個(gè)手機(jī)對(duì)象GALAXY Note Ⅲ為PhoneGalaxyNote3 CellPhone note3=new PhoneGalaxyNote3(); note3.setCellPhoneName("GALAXY Note Ⅲ"); CellPhoneSystem system2=new AndroidSystem(); system2.setSystemName("android4"); system2.setCellPhone(note3);//裝配 system2.systemWorks();//工作 CellPhoneCPU cpu2=new EightCore();//最新8核cpu cpu2.setCpuName("三星Exynos 5 Octa芯片"); cpu2.setCellPhone(note3); cpu2.cpuWorks(); note3.works();//三星GALAXY Note Ⅲ新體驗(yàn) } }
如果需要擴(kuò)展,定義新的維度屬性
package test.design.bridge; public class EightCore extends CellPhoneCPU { }
package test.design.bridge; public class PhoneGalaxyNote3 extends CellPhone{ //具體對(duì)象的屬性與邏輯 }
測(cè)試打印;
I am ios7 system. I am working for this cellphone: IPhone 6 I am cpu. My pattern is:A6 I am working for this cellphone: IPhone 6 --------------------- This cellphone is: IPhone 6 ,welcome to use. This cellphone detail infomation: 系統(tǒng)類型:ios7 cpu型號(hào):A6 --------------------- -----------分割--------------------------- I am android4 system. I am working for this cellphone:GALAXY Note Ⅲ I am cpu. My pattern is:三星Exynos 5 Octa芯片 I am working for this cellphone:GALAXY Note Ⅲ --------------------- This cellphone is:GALAXY Note Ⅲ,welcome to use. This cellphone detail infomation: 系統(tǒng)類型:android4 cpu型號(hào):三星Exynos 5 Octa芯片 ---------------------
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java設(shè)計(jì)實(shí)現(xiàn)一個(gè)針對(duì)各種類型的緩存
這篇文章主要為大家詳細(xì)介紹了Java如何設(shè)計(jì)實(shí)現(xiàn)一個(gè)針對(duì)各種類型的緩存,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-11-11SpringBoot集成RabbitMQ實(shí)現(xiàn)用戶注冊(cè)的示例代碼
這篇文章主要介紹了SpringBoot集成RabbitMQ實(shí)現(xiàn)用戶注冊(cè)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-125種必會(huì)的Java異步調(diào)用轉(zhuǎn)同步的方法你會(huì)幾種
這篇文章主要介紹了5種必會(huì)的Java異步調(diào)用轉(zhuǎn)同步的方法你會(huì)幾種,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12淺談JSON的數(shù)據(jù)交換、緩存問(wèn)題和同步問(wèn)題
這篇文章主要介紹了淺談JSON的數(shù)據(jù)交換、緩存問(wèn)題和同步問(wèn)題,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12spring-security關(guān)閉登錄框的實(shí)現(xiàn)示例
這篇文章主要介紹了spring-security關(guān)閉登錄框的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05java使用軟引用實(shí)現(xiàn)緩存機(jī)制示例
這篇文章主要為大家介紹了java使用軟引用實(shí)現(xiàn)緩存機(jī)制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Spring運(yùn)行時(shí)手動(dòng)注入bean的方法實(shí)例
spring給我們提供了IOC服務(wù),讓我們可以用注解的方式,方便的使用bean的相互引用,下面這篇文章主要給大家介紹了關(guān)于Spring運(yùn)行時(shí)手動(dòng)注入bean的相關(guān)資料,需要的朋友可以參考下2022-05-05詳解SpringBoot如何實(shí)現(xiàn)整合微信登錄
本文主要介紹了SpringBoot實(shí)現(xiàn)整合微信登錄的過(guò)程詳解,文中的示例代碼介紹的非常詳細(xì),對(duì)我們的學(xué)習(xí)過(guò)工作有一定的參考價(jià)值,需要的朋友可以關(guān)注下2021-12-12常用校驗(yàn)注解之@NotNull,@NotBlank,@NotEmpty的區(qū)別及說(shuō)明
這篇文章主要介紹了常用校驗(yàn)注解之@NotNull,@NotBlank,@NotEmpty的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01