You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: bridge/README.md
+19-10
Original file line number
Diff line number
Diff line change
@@ -9,28 +9,34 @@ tags:
9
9
---
10
10
11
11
## Also known as
12
+
12
13
Handle/Body
13
14
14
15
## Intent
16
+
15
17
Decouple an abstraction from its implementation so that the two can vary independently.
16
18
17
19
## Explanation
18
20
19
21
Real world example
20
22
21
-
> Consider you have a weapon with different enchantments and you are supposed to allow mixing different weapons with different enchantments. What would you do? Create multiple copies of each of the weapons for each of the enchantments or would you just create separate enchantment and set it for the weapon as needed? Bridge pattern allows you to do the second.
23
+
> Consider you have a weapon with different enchantments, and you are supposed to allow mixing
24
+
> different weapons with different enchantments. What would you do? Create multiple copies of each
25
+
> of the weapons for each of the enchantments or would you just create separate enchantment and set
26
+
> it for the weapon as needed? Bridge pattern allows you to do the second.
22
27
23
28
In Plain Words
24
29
25
-
> Bridge pattern is about preferring composition over inheritance. Implementation details are pushed from a hierarchy to another object with a separate hierarchy.
30
+
> Bridge pattern is about preferring composition over inheritance. Implementation details are pushed
31
+
> from a hierarchy to another object with a separate hierarchy.
26
32
27
33
Wikipedia says
28
34
29
35
> The bridge pattern is a design pattern used in software engineering that is meant to "decouple an abstraction from its implementation so that the two can vary independently"
30
36
31
37
**Programmatic Example**
32
38
33
-
Translating our weapon example from above. Here we have the `Weapon` hierarchy
39
+
Translating our weapon example from above. Here we have the `Weapon` hierarchy:
34
40
35
41
```java
36
42
publicinterfaceWeapon {
@@ -105,7 +111,7 @@ public class Hammer implements Weapon {
105
111
}
106
112
```
107
113
108
-
And the separate enchantment hierarchy
114
+
Here's the separate enchantment hierarchy:
109
115
110
116
```java
111
117
publicinterfaceEnchantment {
@@ -151,7 +157,7 @@ public class SoulEatingEnchantment implements Enchantment {
151
157
}
152
158
```
153
159
154
-
And both the hierarchies in action
160
+
Here are both hierarchies in action:
155
161
156
162
```java
157
163
var enchantedSword =newSword(newSoulEatingEnchantment());
@@ -178,18 +184,21 @@ hammer.unwield();
178
184
```
179
185
180
186
## Class diagram
187
+
181
188

182
189
183
190
## Applicability
191
+
184
192
Use the Bridge pattern when
185
193
186
-
*you want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time.
187
-
*both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently
188
-
*changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have to be recompiled.
189
-
*you have a proliferation of classes. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term "nested generalizations" to refer to such class hierarchies
190
-
*you want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien's String class, in which multiple objects can share the same string representation.
194
+
*You want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time.
195
+
*Both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently.
196
+
*Changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have to be recompiled.
197
+
*You have a proliferation of classes. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term "nested generalizations" to refer to such class hierarchies.
198
+
*You want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien's String class, in which multiple objects can share the same string representation.
0 commit comments