目的
将抽象与其实现分离,以便两者可以独立变化。
说明
假设你有一个武器具有不同的魔法,你应该想混合不同的武器与不同的魔法。你会怎么做?为每个魔法创造每个武器的多个副本,还是只创建单独的魔法并根据需要为武器设置?桥接模式允许你做第二个。
简而言之
桥模式是关于优先组合而不是继承。实现细节从一个层次结构推送到另一个具有单独层次结构的对象。
维基百科说
桥接模式是软件工程中使用的设计模式,旨在“将抽象与其实现分离,以便两者可以独立变化”
源码示例
以上面的武器为例。
这里我们有Weapon层次结构
<b>public</b> <b>interface</b> Weapon { <b>void</b> wield(); <b>void</b> swing(); <b>void</b> unwield(); Enchantment getEnchantment(); } <b>public</b> <b>class</b> Sword implements Weapon { <b>private</b> <b>final</b> Enchantment enchantment; <b>public</b> Sword(Enchantment enchantment) { <b>this</b>.enchantment = enchantment; } @Override <b>public</b> <b>void</b> wield() { LOGGER.info(<font>"The sword is wielded."</font><font>); enchantment.onActivate(); } @Override <b>public</b> <b>void</b> swing() { LOGGER.info(</font><font>"The sword is swinged."</font><font>); enchantment.apply(); } @Override <b>public</b> <b>void</b> unwield() { LOGGER.info(</font><font>"The sword is unwielded."</font><font>); enchantment.onDeactivate(); } @Override <b>public</b> Enchantment getEnchantment() { <b>return</b> enchantment; } } <b>public</b> <b>class</b> Hammer implements Weapon { <b>private</b> <b>final</b> Enchantment enchantment; <b>public</b> Hammer(Enchantment enchantment) { <b>this</b>.enchantment = enchantment; } @Override <b>public</b> <b>void</b> wield() { LOGGER.info(</font><font>"The hammer is wielded."</font><font>); enchantment.onActivate(); } @Override <b>public</b> <b>void</b> swing() { LOGGER.info(</font><font>"The hammer is swinged."</font><font>); enchantment.apply(); } @Override <b>public</b> <b>void</b> unwield() { LOGGER.info(</font><font>"The hammer is unwielded."</font><font>); enchantment.onDeactivate(); } @Override <b>public</b> Enchantment getEnchantment() { <b>return</b> enchantment; } } </font>
以及单独的魔法等级
<b>public</b> <b>interface</b> Enchantment { <b>void</b> onActivate(); <b>void</b> apply(); <b>void</b> onDeactivate(); } <b>public</b> <b>class</b> FlyingEnchantment implements Enchantment { @Override <b>public</b> <b>void</b> onActivate() { LOGGER.info(<font>"The item begins to glow faintly."</font><font>); } @Override <b>public</b> <b>void</b> apply() { LOGGER.info(</font><font>"The item flies and strikes the enemies finally returning to owner's hand."</font><font>); } @Override <b>public</b> <b>void</b> onDeactivate() { LOGGER.info(</font><font>"The item's glow fades."</font><font>); } } <b>public</b> <b>class</b> SoulEatingEnchantment implements Enchantment { @Override <b>public</b> <b>void</b> onActivate() { LOGGER.info(</font><font>"The item spreads bloodlust."</font><font>); } @Override <b>public</b> <b>void</b> apply() { LOGGER.info(</font><font>"The item eats the soul of enemies."</font><font>); } @Override <b>public</b> <b>void</b> onDeactivate() { LOGGER.info(</font><font>"Bloodlust slowly disappears."</font><font>); } } </font>
两种等级制度都在起作用
Sword enchantedSword = <b>new</b> Sword(<b>new</b> SoulEatingEnchantment()); enchantedSword.wield(); enchantedSword.swing(); enchantedSword.unwield(); <font><i>// The sword is wielded.</i></font><font> </font><font><i>// The item spreads bloodlust.</i></font><font> </font><font><i>// The sword is swinged.</i></font><font> </font><font><i>// The item eats the soul of enemies.</i></font><font> </font><font><i>// The sword is unwielded.</i></font><font> </font><font><i>// Bloodlust slowly disappears.</i></font><font> Hammer hammer = <b>new</b> Hammer(<b>new</b> FlyingEnchantment()); hammer.wield(); hammer.swing(); hammer.unwield(); </font><font><i>// The hammer is wielded.</i></font><font> </font><font><i>// The item begins to glow faintly.</i></font><font> </font><font><i>// The hammer is swinged.</i></font><font> </font><font><i>// The item flies and strikes the enemies finally returning to owner's hand.</i></font><font> </font><font><i>// The hammer is unwielded.</i></font><font> </font><font><i>// The item's glow fades.</i></font><font> </font>
适用场景
何时使用Bridge模式