|
| 1 | +--- |
| 2 | +layout: pattern |
| 3 | +title: Bridge |
| 4 | +folder: bridge |
| 5 | +permalink: /patterns/bridge/ |
| 6 | +categories: Structural |
| 7 | +tags: |
| 8 | + - Gang of Four |
| 9 | +--- |
| 10 | + |
| 11 | +## 又被称为 |
| 12 | + |
| 13 | +手柄/身体模式 |
| 14 | + |
| 15 | +## 目的 |
| 16 | + |
| 17 | +将抽象与其实现分离,以便二者可以独立变化。 |
| 18 | + |
| 19 | +## 解释 |
| 20 | + |
| 21 | +真实世界例子 |
| 22 | + |
| 23 | +> 考虑一下你拥有一种具有不同附魔的武器,并且应该允许将具有不同附魔的不同武器混合使用。 你会怎么做? 为每个附魔创建每种武器的多个副本,还是只是创建单独的附魔并根据需要为武器设置它? 桥接模式使您可以进行第二次操作。 |
| 24 | +
|
| 25 | +通俗的说 |
| 26 | + |
| 27 | +> 桥接模式是一个更推荐组合而不是继承的模式。将实现细节从一个层次结构推送到具有单独层次结构的另一个对象。 |
| 28 | +
|
| 29 | +维基百科说 |
| 30 | + |
| 31 | +> 桥接模式是软件工程中使用的一种设计模式,旨在“将抽象与其实现分离,从而使两者可以独立变化” |
| 32 | +
|
| 33 | +**程序示例** |
| 34 | + |
| 35 | +翻译一下上面的武器示例。下面我们有武器的类层级: |
| 36 | + |
| 37 | +```java |
| 38 | +public interface Weapon { |
| 39 | + void wield(); |
| 40 | + void swing(); |
| 41 | + void unwield(); |
| 42 | + Enchantment getEnchantment(); |
| 43 | +} |
| 44 | + |
| 45 | +public class Sword implements Weapon { |
| 46 | + |
| 47 | + private final Enchantment enchantment; |
| 48 | + |
| 49 | + public Sword(Enchantment enchantment) { |
| 50 | + this.enchantment = enchantment; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void wield() { |
| 55 | + LOGGER.info("The sword is wielded."); |
| 56 | + enchantment.onActivate(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void swing() { |
| 61 | + LOGGER.info("The sword is swinged."); |
| 62 | + enchantment.apply(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void unwield() { |
| 67 | + LOGGER.info("The sword is unwielded."); |
| 68 | + enchantment.onDeactivate(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Enchantment getEnchantment() { |
| 73 | + return enchantment; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +public class Hammer implements Weapon { |
| 78 | + |
| 79 | + private final Enchantment enchantment; |
| 80 | + |
| 81 | + public Hammer(Enchantment enchantment) { |
| 82 | + this.enchantment = enchantment; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void wield() { |
| 87 | + LOGGER.info("The hammer is wielded."); |
| 88 | + enchantment.onActivate(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void swing() { |
| 93 | + LOGGER.info("The hammer is swinged."); |
| 94 | + enchantment.apply(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void unwield() { |
| 99 | + LOGGER.info("The hammer is unwielded."); |
| 100 | + enchantment.onDeactivate(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Enchantment getEnchantment() { |
| 105 | + return enchantment; |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +这里是单独的附魔类结构: |
| 111 | + |
| 112 | +```java |
| 113 | +public interface Enchantment { |
| 114 | + void onActivate(); |
| 115 | + void apply(); |
| 116 | + void onDeactivate(); |
| 117 | +} |
| 118 | + |
| 119 | +public class FlyingEnchantment implements Enchantment { |
| 120 | + |
| 121 | + @Override |
| 122 | + public void onActivate() { |
| 123 | + LOGGER.info("The item begins to glow faintly."); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void apply() { |
| 128 | + LOGGER.info("The item flies and strikes the enemies finally returning to owner's hand."); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void onDeactivate() { |
| 133 | + LOGGER.info("The item's glow fades."); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +public class SoulEatingEnchantment implements Enchantment { |
| 138 | + |
| 139 | + @Override |
| 140 | + public void onActivate() { |
| 141 | + LOGGER.info("The item spreads bloodlust."); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void apply() { |
| 146 | + LOGGER.info("The item eats the soul of enemies."); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void onDeactivate() { |
| 151 | + LOGGER.info("Bloodlust slowly disappears."); |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +这里是两种层次结构的实践: |
| 157 | + |
| 158 | +```java |
| 159 | +var enchantedSword = new Sword(new SoulEatingEnchantment()); |
| 160 | +enchantedSword.wield(); |
| 161 | +enchantedSword.swing(); |
| 162 | +enchantedSword.unwield(); |
| 163 | +// The sword is wielded. |
| 164 | +// The item spreads bloodlust. |
| 165 | +// The sword is swinged. |
| 166 | +// The item eats the soul of enemies. |
| 167 | +// The sword is unwielded. |
| 168 | +// Bloodlust slowly disappears. |
| 169 | + |
| 170 | +var hammer = new Hammer(new FlyingEnchantment()); |
| 171 | +hammer.wield(); |
| 172 | +hammer.swing(); |
| 173 | +hammer.unwield(); |
| 174 | +// The hammer is wielded. |
| 175 | +// The item begins to glow faintly. |
| 176 | +// The hammer is swinged. |
| 177 | +// The item flies and strikes the enemies finally returning to owner's hand. |
| 178 | +// The hammer is unwielded. |
| 179 | +// The item's glow fades. |
| 180 | +``` |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +## 类图 |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | +## 适用性 |
| 189 | + |
| 190 | +使用桥接模式当 |
| 191 | + |
| 192 | +* 你想永久性的避免抽象和他的实现之间的绑定。有可能是这种情况,当实现需要被选择或者在运行时切换。 |
| 193 | +* 抽象和他们的实现应该能通过写子类来扩展。这种情况下,桥接模式让你可以组合不同的抽象和实现并独立的扩展他们。 |
| 194 | +* 对抽象的实现的改动应当不会对客户产生影响;也就是说,他们的代码不必重新编译。 |
| 195 | +* 你有种类繁多的类。这样的类层次结构表明需要将一个对象分为两部分。Rumbaugh 使用术语“嵌套归纳”来指代这种类层次结构。 |
| 196 | +* 你想在多个对象间分享一种实现(可能使用引用计数),这个事实应该对客户隐藏。一个简单的示例是Coplien的String类,其中多个对象可以共享同一字符串表示形式 |
| 197 | + |
| 198 | +## 教程 |
| 199 | + |
| 200 | +* [Bridge Pattern Tutorial](https://www.journaldev.com/1491/bridge-design-pattern-java) |
| 201 | + |
| 202 | +## 鸣谢 |
| 203 | + |
| 204 | +* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59) |
| 205 | +* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b) |
0 commit comments