天天看點

java 橋接模式_Java設計模式之橋接模式執行個體詳解

本文執行個體講述了Java設計模式之橋接模式。分享給大家供大家參考,具體如下:

概念:

橋接模式(Bridge Pattern):将抽象部分與它的實作部分分離,使它們都可以獨立地變化。

橋接模式将繼承關系轉換為關聯關系,進而降低了類與類之間的耦合,減少了代碼編寫量。

什麼情況下會用橋接模式?

簡單的說就是我們在抽象對象的特征時,對象的特征屬性又很抽象,不得不把屬性再次抽象。

否則的話,具體子類的數量将會成幾何增長,而且不易擴充。沒辦法維護現有代碼。

舉例,我們在抽象手機這二個對象時,它的幾個屬性,如作業系統,cpu,螢幕,營運商網絡等都很複雜。我們不能簡單的把這幾個屬性直接定義,必須再次抽象化。而具體的一個手機對象就是這些屬性的組合,但不是簡單的組合,屬性需要實作自己作為屬性的功能。在這樣的設計下,代碼的維護和擴充也就容易了。

注意:在說這個模式的時候,我不能保證說的和寫得例子都是正确的,畢竟我也是新接觸到,所有例子均基于與個人了解。

我認為的橋接模式說明圖:

java 橋接模式_Java設計模式之橋接模式執行個體詳解

下面是例子:

1. 首先定義抽象類,抽象和描述對象的特征。

在對象的屬性上劃分次元,為了以後橋接和擴充。

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("系統類型:"+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. 屬性次元的抽象。(可以使用接口定義,關鍵看你的具體功能)

package test.design.bridge;

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);// 裝配(橋接,或者可以認為對象類與其屬性類的傳遞)

}

public String getCpuName() {

return cpuName;

}

public void setCpuName(String cpuName) {

this.cpuName = cpuName;

}

}

package test.design.bridge;

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);// 裝配(橋接,或者可以認為對象類與其屬性類的傳遞)

}

public String getSystemName() {

return SystemName;

}

public void setSystemName(String systemName) {

SystemName = systemName;

}

}

3. 具體的次元屬性對象。

這裡我們在作業系統屬性和cpu屬性上各定義2個具體對象,

package test.design.bridge;

public class AndroidSystem extends CellPhoneSystem{

}

package test.design.bridge;

public class IOSSystem extends CellPhoneSystem{

}

package test.design.bridge;

public class TwoCore extends CellPhoneCPU{

}

package test.design.bridge;

public class FourCore extends CellPhoneCPU{

}

4. 測試代碼。

其中說了在需要擴充次元的情況下,怎麼擴充的。

定義一個手機對象

package test.design.bridge;

public class Phone1 extends CellPhone{

//具體對象的屬性與邏輯

}

測試main函數

package test.design.bridge;

public class TestMain {

public static void main(String[] args) {

//任何一種具體的對象都是複雜多種屬性的集合,在此可以看出橋接模式在建構對象時的靈活性

//産生一個具體對象1

CellPhone p1=new Phone1();

p1.setCellPhoneName(" IPhone 6 ");

CellPhoneSystem system1=new IOSSystem();//作業系統屬性次元

system1.setSystemName("ios7");

system1.setCellPhone(p1);//裝配

system1.systemWorks();//工作

CellPhoneCPU cpu1=new TwoCore();//cpu屬性次元

cpu1.setCpuName("A6");

cpu1.setCellPhone(p1);

cpu1.cpuWorks();

p1.works();//最終整體對象功能

System.out.println("-----------分割---------------------------");

//在cpu次元上擴充。比如出現新型cpu:8核三星Exynos 5 Octa晶片".

//三星手機推出了GALAXY Note Ⅲ就是使用這種新型cpu. 寫一個新類EightCore擴充cpu次元.

//同時定義這個手機對象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 Ⅲ新體驗

}

}

如果需要擴充,定義新的次元屬性

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:

系統類型: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:

系統類型:android4

cpu型号:三星Exynos 5 Octa晶片

---------------------

希望本文所述對大家java程式設計有所幫助。