内容简介:将抽象与其实现分离,以便两者可以独立变化。
目的
将抽象与其实现分离,以便两者可以独立变化。
说明
假设你有一个武器具有不同的魔法,你应该想混合不同的武器与不同的魔法。你会怎么做?为每个魔法创造每个武器的多个副本,还是只创建单独的魔法并根据需要为武器设置?桥接模式允许你做第二个。
简而言之
桥模式是关于优先组合而不是继承。实现细节从一个层次结构推送到另一个具有单独层次结构的对象。
维基百科说
桥接模式是软件工程中使用的设计模式,旨在“将抽象与其实现分离,以便两者可以独立变化”
源码示例
以上面的武器为例。
这里我们有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类,其中多个对象可以共享相同的字符串表示。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
白话机器学习算法
[新加坡] 黄莉婷、[新加坡] 苏川集 / 武传海 / 人民邮电出版社 / 2019-2 / 49.00元
与使用数学语言或计算机编程语言讲解算法的书不同,本书另辟蹊径,用通俗易懂的人类语言以及大量有趣的示例和插图讲解10多种前沿的机器学习算法。内容涵盖k均值聚类、主成分分析、关联规则、社会网络分析等无监督学习算法,以及回归分析、k最近邻、支持向量机、决策树、随机森林、神经网络等监督学习算法,并概述强化学习算法的思想。任何对机器学习和数据科学怀有好奇心的人都可以通过本书构建知识体系。一起来看看 《白话机器学习算法》 这本书的介绍吧!