Java設(shè)計模式之橋接模式實例詳解
本文實例講述了Java設(shè)計模式之橋接模式。分享給大家供大家參考,具體如下:
概念:
橋接模式(Bridge Pattern):將抽象部分與它的實現(xiàn)部分分離,使它們都可以獨立地變化。
橋接模式將繼承關(guān)系轉(zhuǎn)換為關(guān)聯(lián)關(guān)系,從而降低了類與類之間的耦合,減少了代碼編寫量。
什么情況下會用橋接模式?
簡單的說就是我們在抽象對象的特征時,對象的特征屬性又很抽象,不得不把屬性再次抽象。
否則的話,具體子類的數(shù)量將會成幾何增長,而且不易擴(kuò)展。沒辦法維護(hù)現(xiàn)有代碼。
舉例,我們在抽象手機(jī)這二個對象時,它的幾個屬性,如操作系統(tǒng),cpu,屏幕,運營商網(wǎng)絡(luò)等都很復(fù)雜。我們不能簡單的把這幾個屬性直接定義,必須再次抽象化。而具體的一個手機(jī)對象就是這些屬性的組合,但不是簡單的組合,屬性需要實現(xiàn)自己作為屬性的功能。在這樣的設(shè)計下,代碼的維護(hù)和擴(kuò)展也就容易了。
注意:在說這個模式的時候,我不能保證說的和寫得例子都是正確的,畢竟我也是新接觸到,所有例子均基于與個人理解。
我認(rèn)為的橋接模式說明圖:

下面是例子:
1. 首先定義抽象類,抽象和描述對象的特征。
在對象的屬性上劃分維度,為了以后橋接和擴(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型號:"+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被抽象成一個維度,為了以后擴(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)為對象類與其屬性類的傳遞)
}
public String getCpuName() {
return cpuName;
}
public void setCpuName(String cpuName) {
this.cpuName = cpuName;
}
}
package test.design.bridge;
/**
* 屬性操作系統(tǒng)被抽象成一個維度,為了以后擴(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)為對象類與其屬性類的傳遞)
}
public String getSystemName() {
return SystemName;
}
public void setSystemName(String systemName) {
SystemName = systemName;
}
}
3. 具體的維度屬性對象。
這里我們在操作系統(tǒng)屬性和cpu屬性上各定義2個具體對象,
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. 測試代碼。
其中說了在需要擴(kuò)展維度的情況下,怎么擴(kuò)展的。
定義一個手機(jī)對象
package test.design.bridge;
public class Phone1 extends CellPhone{
//具體對象的屬性與邏輯
}
測試main函數(shù)
package test.design.bridge;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
//任何一種具體的對象都是復(fù)雜多種屬性的集合,在此可以看出橋接模式在構(gòu)建對象時的靈活性
//產(chǎn)生一個具體對象1
CellPhone p1=new Phone1();
p1.setCellPhoneName(" IPhone 6 ");
CellPhoneSystem system1=new IOSSystem();//操作系統(tǒng)屬性維度
system1.setSystemName("ios7");
system1.setCellPhone(p1);//裝配
system1.systemWorks();//工作
/*裝配說的簡單點就是傳值。因為我們把一個對象的屬性按維度分開來了,
那么橋接的時候就必須相互傳遞對象。即對象類可以調(diào)用子屬相類對象,
子屬性類對象也可以調(diào)用該對象類.
關(guān)于這樣的傳值方式有多種,你可以在構(gòu)造函數(shù)中傳遞,也可以在
調(diào)用具體邏輯方法時傳遞。這里我直接用set方法傳遞,只是為了更清楚.
如果某個屬性維度是必須出現(xiàn)的,那就可以在抽象類的構(gòu)造函數(shù)中傳入*/
CellPhoneCPU cpu1=new TwoCore();//cpu屬性維度
cpu1.setCpuName("A6");
cpu1.setCellPhone(p1);
cpu1.cpuWorks();
p1.works();//最終整體對象功能
/*
橋接模式就是為了應(yīng)對屬性的擴(kuò)展,在此說的屬性必須是在維度確定的情況下。
比如,這里我們在定義手機(jī)對象時,確定兩個屬性維度:操作系統(tǒng)和cpu型號。
以后再這兩個屬性中,需要擴(kuò)展時,就可以使用該模式。比如,一種新的cpu
型號出現(xiàn)了,那么我不用重新設(shè)計現(xiàn)在的代碼,只要增添一個cpu類即可。
如果出現(xiàn)了新的維度屬性,比如手機(jī)對象必須考慮屏幕大小。那橋接模式
在此就需要從根本上修改代碼來了。
*/
System.out.println("-----------分割---------------------------");
//在cpu維度上擴(kuò)展。比如出現(xiàn)新型cpu:8核三星Exynos 5 Octa芯片".
//三星手機(jī)推出了GALAXY Note Ⅲ就是使用這種新型cpu. 寫一個新類EightCore擴(kuò)展cpu維度.
//同時定義這個手機(jī)對象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 Ⅲ新體驗
}
}
如果需要擴(kuò)展,定義新的維度屬性
package test.design.bridge;
public class EightCore extends CellPhoneCPU {
}
package test.design.bridge;
public class PhoneGalaxyNote3 extends CellPhone{
//具體對象的屬性與邏輯
}
測試打印;
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型號: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型號:三星Exynos 5 Octa芯片 ---------------------
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Java虛擬機(jī)JVM之server模式與client模式的區(qū)別
這篇文章主要介紹了Java虛擬機(jī)JVM的client模式和Server模式兩者的區(qū)別和聯(lián)系2017-12-12
IntelliJ IDEA 2018 最新激活碼(截止到2018年1月30日)
這篇文章主要介紹了IntelliJ IDEA 2018 最新激活碼(截止到2018年1月30日)的相關(guān)資料,需要的朋友可以參考下2018-01-01
Java中ShardingSphere 數(shù)據(jù)分片的實現(xiàn)
其實很多人對分庫分表多少都有點恐懼,我們今天用ShardingSphere 給大家演示數(shù)據(jù)分片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09

