Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1981e4d

Browse files
author
deeper
committed
add bridge
1 parent bc61f0c commit 1981e4d

File tree

7 files changed

+114
-0
lines changed

7 files changed

+114
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.ruoxu.pattern.bridge;
2+
/**
3+
* 实现部分
4+
*/
5+
public interface Additives {
6+
String addSomething();
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ruoxu.pattern.bridge;
2+
/**
3+
* 抽象部分
4+
*/
5+
public abstract class Coffee {
6+
protected Additives impl;
7+
8+
public Coffee(Additives impl) {
9+
this.impl = impl;
10+
}
11+
12+
public abstract void makeCoffee();
13+
}

‎src/com/ruoxu/pattern/bridge/Demo.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.ruoxu.pattern.bridge;
2+
/**
3+
* 桥接模式(用的不多,缺点:不容易设计)桥->简而言之其作用就是连接河的两边。
4+
* 定义:将抽象部分和实现部分 分离(即解耦),使它们都可以独立地进行变化。这个概念很难理解。
5+
* 这里的抽象和实现指的并不是 抽象类和其派生类的分离,这没有任何意义。
6+
* 定义的通俗理解:系统可能有多角度分类,每种分类都有可能变化,那么就把这种多角度分离出来(角度1和角度2分离<==>抽象和实现分离)让它们独立变化,减少它们之间的耦合。
7+
* 根据UML图,此设计模式的抽象部分和实现部分就是指 角度1和角度2,该概念也让人难以理解,直接看代码即可。
8+
*
9+
* 多继承和桥接模式比较:
10+
* 桥接模式用一种巧妙的方式处理多层继承存在的问题,用抽象关联取代了传统的多层继承,多继承方案往往违背了类的单一职责原则(即一个类只有一个变化的原则),
11+
* 且多继承方案的复用性比较差。如果2*2则需要4个类来继承,Bridge模式是比多继承方案更好的解决方法。
12+
* 桥接模式将继承关系转化成关联关系,它降低了类与类之间的耦合度,减少了系统中类的数量,也减少了代码量。
13+
*
14+
* 使用场景:
15+
* 任何多维变化或多个树状类之间的耦合都可以使用桥接模式来实现解耦。对于两个独立变化的维度,使用桥接模式再适合不过了。
16+
*/
17+
public class Demo {
18+
public static void main(String[] args) {
19+
// 原汁原味
20+
OrdinaryAdditives implOrdinary= new OrdinaryAdditives();
21+
// 加糖
22+
SugerAdditives implSuger= new SugerAdditives();
23+
24+
LargeCoffee largeOridinaryCoffee = new LargeCoffee(implOrdinary);
25+
largeOridinaryCoffee.makeCoffee();
26+
27+
LargeCoffee largeSugerCoffee = new LargeCoffee(implSuger);
28+
largeSugerCoffee.makeCoffee();
29+
30+
SmallCoffee smallOridinaryCoffee = new SmallCoffee(implOrdinary);
31+
smallOridinaryCoffee.makeCoffee();
32+
33+
SmallCoffee smallSugerCoffee = new SmallCoffee(implSuger);
34+
smallSugerCoffee.makeCoffee();
35+
36+
37+
}
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ruoxu.pattern.bridge;
2+
/**
3+
* 抽象部分的子类
4+
*/
5+
public class LargeCoffee extends Coffee{
6+
7+
public LargeCoffee(Additives impl) {
8+
super(impl);
9+
}
10+
11+
@Override
12+
public void makeCoffee() {
13+
System.out.println("大杯的"+impl.addSomething()+"咖啡");
14+
}
15+
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ruoxu.pattern.bridge;
2+
/**
3+
* 实现部分的具体实现
4+
*/
5+
public class OrdinaryAdditives implements Additives{
6+
7+
@Override
8+
public String addSomething() {
9+
return "原味";
10+
}
11+
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ruoxu.pattern.bridge;
2+
/**
3+
* 抽象部分的子类
4+
*/
5+
public class SmallCoffee extends Coffee{
6+
7+
public SmallCoffee(Additives impl) {
8+
super(impl);
9+
}
10+
11+
@Override
12+
public void makeCoffee() {
13+
System.out.println("小杯的"+impl.addSomething()+"咖啡");
14+
}
15+
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ruoxu.pattern.bridge;
2+
/**
3+
* 实现部分的具体实现
4+
*/
5+
public class SugerAdditives implements Additives{
6+
7+
@Override
8+
public String addSomething() {
9+
return "加糖";
10+
}
11+
12+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /