本文實例講述了java使用橋接模式實現開關和電燈照明功能。分享給大家供大家參考,具體如下:
一、模式定義
橋接模式,也稱橋梁模式,在軟件系統中,由于自身的邏輯,具有兩個或多個維度的變化,如何應對這種多維度的變化,橋接模式使得軟件系統能夠輕松地沿著多個方向進行變化,而又不引入額外的復雜度。
橋接模式三個關鍵詞為:抽象化,實現化,脫耦
二、模式舉例
1 橋接模式分析方法
我們借用電燈照明來說明該模式。
不使用繼承,使用對象組合的方式,將開關和電燈的強關聯關系變成弱關聯關系。
2 橋接模式靜態類模型
3 代碼示例
3.1 創建電燈接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.demo.bridge.lights; /** * 電燈接口 * * @author * */ public interface ilight { // 接通電流 public void electricconnected(); // 照明 public void light(); // 電流關閉 public void electricclosed(); } |
3.2 創建一般開關
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package com.demo.bridge.switchs; import com.demo.bridge.lights.ilight; /** * 開關頂層類 * * @author * */ public class baseswitch { // 使用組合 設置ilight為內部私有屬性 此為橋梁 protected ilight light; // 構造方法將 外部的light類型注入進來 public baseswitch(ilight light) { this .light = light; } /** * 開燈方法 */ public final void makelight() { // 打開開關 接通電流 this .light.electricconnected(); // 照明 this .light.light(); // 關閉開關 關閉電流 this .light.electricclosed(); } } |
3.3 創建遙控開關
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package com.demo.bridge.switchs.sub; import com.demo.bridge.lights.ilight; import com.demo.bridge.switchs.baseswitch; /** * 遙控開關 繼承baseswitch 擴展功能 * * @author * */ public class remotecontrolswitch extends baseswitch { // 構造方法 public remotecontrolswitch(ilight light) { super (light); } /** * 使用遙控開關控制開燈 * * @param opercolor * 燈顏色 */ public final void makeremotelight( int opercolor) { // 打開開關 接通電流 this .light.electricconnected(); // 照明 this .light.light(); string color = "" ; switch (opercolor) { case 1 : color = "暖色" ; break ; case 2 : color = "藍色" ; break ; case 3 : color = "紅色" ; break ; default : color = "白色" ; break ; } system.out.println( " ...現在是" + color + "!" ); // 關閉開關 關閉電流 this .light.electricclosed(); } } |
3.4 白熾燈實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package com.demo.bridge.lights.impl; import com.demo.bridge.lights.ilight; /** * 白熾燈 實現 * * @author * */ public class incandescentlight implements ilight { // 電流關閉 public void electricclosed() { system.out.println( "白熾燈被關閉了..." ); } // 接通電流 public void electricconnected() { system.out.println( "白熾燈被打開了..." ); } // 照明 public void light() { system.out.println( "白熾燈照明!" ); } } |
3.5 水晶燈實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package com.demo.bridge.lights.impl; import com.demo.bridge.lights.ilight; /** * 水晶燈 實現 * * @author * */ public class crystallight implements ilight { // 電流關閉 public void electricclosed() { system.out.println( "水晶燈被關閉了..." ); } // 接通電流 public void electricconnected() { system.out.println( "水晶燈被打開了..." ); } // 照明 public void light() { system.out.println( "水晶燈照明!" ); } } |
3.6 一般開關控制白熾燈,遙控開關控制水晶燈
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.demo; import com.demo.bridge.lights.ilight; import com.demo.bridge.lights.impl.crystallight; import com.demo.bridge.lights.impl.incandescentlight; import com.demo.bridge.switchs.baseswitch; import com.demo.bridge.switchs.sub.remotecontrolswitch; /** * 客戶端應用程序 * * @author * */ public class clientforbridge { /** * @param args */ public static void main(string[] args) { // 白熾燈 實例 ilight incandescentlight = new incandescentlight(); // 水晶燈 實例 ilight crystallight = new crystallight(); // 一般開關 system.out.println( "-- 一般開關 -- " ); baseswitch switch1 = new baseswitch(incandescentlight); switch1.makelight(); system.out.println( "\n-- 遙控開關 -- " ); // 遙控開關 remotecontrolswitch remotecontrolswitch = new remotecontrolswitch( crystallight); remotecontrolswitch.makeremotelight( 1 ); } } |
運行結果:
-- 一般開關 --
白熾燈被打開了...
白熾燈照明!
白熾燈被關閉了...
-- 遙控開關 --
水晶燈被打開了...
水晶燈照明!
...現在是暖色!
水晶燈被關閉了...
3.7 一般開關控制水晶燈,遙控開關控制白熾燈
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.demo; import com.demo.bridge.lights.ilight; import com.demo.bridge.lights.impl.crystallight; import com.demo.bridge.lights.impl.incandescentlight; import com.demo.bridge.switchs.baseswitch; import com.demo.bridge.switchs.sub.remotecontrolswitch; /** * 客戶端應用程序 * * @author * */ public class clientforbridge { /** * @param args */ public static void main(string[] args) { // 白熾燈 實例 ilight incandescentlight = new incandescentlight(); // 水晶燈 實例 ilight crystallight = new crystallight(); // 一般開關 system.out.println( "-- 一般開關 -- " ); baseswitch switch1 = new baseswitch(crystallight); switch1.makelight(); system.out.println( "\n-- 遙控開關 -- " ); // 遙控開關 remotecontrolswitch remotecontrolswitch = new remotecontrolswitch( incandescentlight); remotecontrolswitch.makeremotelight( 1 ); } } |
運行結果
-- 一般開關 --
水晶燈被打開了...
水晶燈照明!
水晶燈被關閉了...
-- 遙控開關 --
白熾燈被打開了...
白熾燈照明!
...現在是暖色!
白熾燈被關閉了...
三、設計原則
1 盡量使用對象聚合弱關聯,避免使用繼承強關聯。
2 抽象化和實現化脫耦。
四、使用場合
1 不希望在抽象類和實現部分之間有一個固定的綁定關系
2 類的抽象及實現部分都應該可以通過孑類的方法加以擴充
3 對一個抽象的實現部分的修改對客戶不產生影響,即客戶代碼不必重新編譯
五、橋接模式靜態類圖
希望本文所述對大家java程序設計有所幫助。
原文鏈接:https://blog.csdn.net/chengqiuming/article/details/70140539