桥(Bridge)

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

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

目的

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

说明

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

简而言之

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

维基百科说

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

源码示例

以上面的武器为例。

这里我们有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类,其中多个对象可以共享相同的字符串表示。

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

查看所有标签

猜你喜欢:

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

Head First Rails

Head First Rails

David Griffiths / O'Reilly Media / 2008-12-30 / USD 49.99

Figure its about time that you hop on the Ruby on Rails bandwagon? You've heard that it'll increase your productivity exponentially, and allow you to created full fledged web applications with minimal......一起来看看 《Head First Rails》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码