桥(Bridge)

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

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

目的

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

说明

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

简而言之

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

维基百科说

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

源码示例

以上面的武器为例。

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

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

查看所有标签

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

计算机组成(第 6 版)

计算机组成(第 6 版)

Andrew S. Tanenbaum、Todd Austin / 刘卫东、宋佳兴 / 机械工业出版社 / 2014-8-19 / CNY 99.00

本书采用结构化方法来介绍计算机系统,书的内容完全建立在“计算机是由层次结构组成的,每层完成规定的功能”这一概念之上。作者对本版进行了彻底的更新,以反映当今最重要的计算机技术以及计算机组成和体系结构方面的最新进展。书中详细讨论了数字逻辑层、微体系结构层、指令系统层、操作系统层和汇编语言层,并涵盖了并行体系结构的内容,而且每一章结尾都配有丰富的习题。本书适合作为计算机专业本科生计算机组成与结构课程的教......一起来看看 《计算机组成(第 6 版)》 这本书的介绍吧!

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

Base64 编码/解码

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

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具