桥(Bridge)

栏目: 后端 · 发布时间: 5年前

内容简介:将抽象与其实现分离,以便两者可以独立变化。

目的

将抽象与其实现分离,以便两者可以独立变化。

说明

假设你有一个武器具有不同的魔法,你应该想混合不同的武器与不同的魔法。你会怎么做?为每个魔法创造每个武器的多个副本,还是只创建单独的魔法并根据需要为武器设置?桥接模式允许你做第二个。

简而言之

桥模式是关于优先组合而不是继承。实现细节从一个层次结构推送到另一个具有单独层次结构的对象。

维基百科说

桥接模式是软件工程中使用的设计模式,旨在“将抽象与其实现分离,以便两者可以独立变化”

源码示例

以上面的武器为例。

这里我们有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模式

  • 您希望避免抽象与其实现之间的永久绑定。例如,必须在运行时选择或切换实现时可能就是这种情况。
  • 抽象及其实现都应该通过子类化来扩展。在这种情况下,Bridge模式允许您组合不同的抽象和实现并独立扩展它们
  • 抽象实现的变化应该对客户端没有影响; 也就是说,不必重新编译他们的代码。
  • 你有很多类。这样的类层次结构表明需要将对象分成两部分。Rumbaugh使用术语“嵌套的泛化”来引用这样的类层次结构
  • 你想在多个对象之间共享一个实现(可能使用引用计数),并从客户端隐藏这一事实。一个简单的例子是Coplien的String类,其中多个对象可以共享相同的字符串表示。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Google成功的七堂课

Google成功的七堂课

罗耀宗 / 电子工业出版社 / 2005-7 / 28.00元

Google是全球使用人数最多的搜索引擎,在短短几年内,Google从斯坦福大学的实验室,茁壮成长为举世瞩目的IT业超级巨人,他们的成功绝非偶然,尤其是在网络泡沫破灭,行业一片萧条之际,它的崛起更为IT业带来一缕曙光。作者从趋势观察家的角度,以讲座的形式,向读者讲述Google成功的关键因素:破除因循守旧、不断打破常规,核心技术领先、做出了“更好的捕鼠器”,使得Google在搜索技术方面远远超越对......一起来看看 《Google成功的七堂课》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换