🔍目的
將抽象與其實現分離,以便二者可以獨立變化。
🔍解釋
真實世界例子
考慮一下你擁有一種具有不同附魔的武器,并且應該允許將具有不同附魔的不同武器混合使用。 你會怎么做? 為每個附魔創建每種武器的多個副本,還是只是創建單獨的附魔并根據需要為武器設置它? 橋接模式使您可以進行第二次操作
通俗描述
橋接模式是一個更推薦組合而不是繼承的模式。將實現細節從一個層次結構推送到具有單獨層次結構的另一個對象。
維基百科
橋接模式是軟件工程中使用的一種設計模式,旨在“將抽象與其實現分離,從而使兩者可以獨立變化”
程序示例
創建一個武器類層級
public interface Weapon {void wield();void swing();void unwield();Enchantment getEnchantment();
}public class Sword implements Weapon {private final Enchantment enchantment;public Sword(Enchantment enchantment) {this.enchantment = enchantment;}@Overridepublic void wield() {LOGGER.info("The sword is wielded.");enchantment.onActivate();}@Overridepublic void swing() {LOGGER.info("The sword is swinged.");enchantment.apply();}@Overridepublic void unwield() {LOGGER.info("The sword is unwielded.");enchantment.onDeactivate();}@Overridepublic Enchantment getEnchantment() {return enchantment;}
}public class Hammer implements Weapon {private final Enchantment enchantment;public Hammer(Enchantment enchantment) {this.enchantment = enchantment;}@Overridepublic void wield() {LOGGER.info("The hammer is wielded.");enchantment.onActivate();}@Overridepublic void swing() {LOGGER.info("The hammer is swinged.");enchantment.apply();}@Overridepublic void unwield() {LOGGER.info("The hammer is unwielded.");enchantment.onDeactivate();}@Overridepublic Enchantment getEnchantment() {return enchantment;}
}
創建單獨的附魔類
public interface Enchantment {void onActivate();void apply();void onDeactivate();
}public class FlyingEnchantment implements Enchantment {@Overridepublic void onActivate() {LOGGER.info("The item begins to glow faintly.");}@Overridepublic void apply() {LOGGER.info("The item flies and strikes the enemies finally returning to owner's hand.");}@Overridepublic void onDeactivate() {LOGGER.info("The item's glow fades.");}
}public class SoulEatingEnchantment implements Enchantment {@Overridepublic void onActivate() {LOGGER.info("The item spreads bloodlust.");}@Overridepublic void apply() {LOGGER.info("The item eats the soul of enemies.");}@Overridepublic void onDeactivate() {LOGGER.info("Bloodlust slowly disappears.");}
}
原神啟動!
var enchantedSword = new Sword(new SoulEatingEnchantment());
enchantedSword.wield();
enchantedSword.swing();
enchantedSword.unwield();
// The sword is wielded.
// The item spreads bloodlust.
// The sword is swinged.
// The item eats the soul of enemies.
// The sword is unwielded.
// Bloodlust slowly disappears.var hammer = new Hammer(new FlyingEnchantment());
hammer.wield();
hammer.swing();
hammer.unwield();
// The hammer is wielded.
// The item begins to glow faintly.
// The hammer is swinged.
// The item flies and strikes the enemies finally returning to owner's hand.
// The hammer is unwielded.
// The item's glow fades.
🔍類圖
Bridge class diagram?
🔍適用場景
使用橋接模式當
- 你想永久性的避免抽象和他的實現之間的綁定。有可能是這種情況,當實現需要被選擇或者在運行時切換。
- 抽象和他們的實現應該能通過寫子類來擴展。這種情況下,橋接模式讓你可以組合不同的抽象和實現并獨立的擴展他們。
- 對抽象的實現的改動應當不會對客戶產生影響;也就是說,他們的代碼不必重新編譯。
- 你有種類繁多的類。這樣的類層次結構表明需要將一個對象分為兩部分。Rumbaugh 使用術語“嵌套歸納”來指代這種類層次結構。
- 你想在多個對象間分享一種實現(可能使用引用計數),這個事實應該對客戶隱藏。一個簡單的示例是Coplien的String類,其中多個對象可以共享同一字符串表示形式