Skip to content

Commit f80d917

Browse files
authored
Merge pull request iluwatar#619 from iluwatar/bridge
Improved bridge example and added explanation
2 parents ed1a002 + 1dc038b commit f80d917

21 files changed

+294
-685
lines changed

bridge/README.md

Lines changed: 163 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,170 @@ tags:
1515
Handle/Body
1616

1717
## Intent
18-
Decouple an abstraction from its implementation so that the two can
19-
vary independently.
18+
Decouple an abstraction from its implementation so that the two can vary independently.
2019

21-
![alt text](./etc/bridge.png "Bridge")
20+
## Explanation
21+
22+
Real world example
23+
24+
> 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.
25+
26+
In Plain Words
27+
28+
> Bridge pattern is about preferring composition over inheritance. Implementation details are pushed from a hierarchy to another object with a separate hierarchy.
29+
30+
Wikipedia says
31+
32+
> 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"
33+
34+
**Programmatic Example**
35+
36+
Translating our weapon example from above. Here we have the `Weapon` hierarchy
37+
38+
```
39+
public interface Weapon {
40+
void wield();
41+
void swing();
42+
void unwield();
43+
Enchantment getEnchantment();
44+
}
45+
46+
public class Sword implements Weapon {
47+
48+
private final Enchantment enchantment;
49+
50+
public Sword(Enchantment enchantment) {
51+
this.enchantment = enchantment;
52+
}
53+
54+
@Override
55+
public void wield() {
56+
LOGGER.info("The sword is wielded.");
57+
enchantment.onActivate();
58+
}
59+
60+
@Override
61+
public void swing() {
62+
LOGGER.info("The sword is swinged.");
63+
enchantment.apply();
64+
}
65+
66+
@Override
67+
public void unwield() {
68+
LOGGER.info("The sword is unwielded.");
69+
enchantment.onDeactivate();
70+
}
71+
72+
@Override
73+
public Enchantment getEnchantment() {
74+
return enchantment;
75+
}
76+
}
77+
78+
public class Hammer implements Weapon {
79+
80+
private final Enchantment enchantment;
81+
82+
public Hammer(Enchantment enchantment) {
83+
this.enchantment = enchantment;
84+
}
85+
86+
@Override
87+
public void wield() {
88+
LOGGER.info("The hammer is wielded.");
89+
enchantment.onActivate();
90+
}
91+
92+
@Override
93+
public void swing() {
94+
LOGGER.info("The hammer is swinged.");
95+
enchantment.apply();
96+
}
97+
98+
@Override
99+
public void unwield() {
100+
LOGGER.info("The hammer is unwielded.");
101+
enchantment.onDeactivate();
102+
}
103+
104+
@Override
105+
public Enchantment getEnchantment() {
106+
return enchantment;
107+
}
108+
}
109+
```
110+
111+
And the separate enchantment hierarchy
112+
113+
```
114+
public interface Enchantment {
115+
void onActivate();
116+
void apply();
117+
void onDeactivate();
118+
}
119+
120+
public class FlyingEnchantment implements Enchantment {
121+
122+
@Override
123+
public void onActivate() {
124+
LOGGER.info("The item begins to glow faintly.");
125+
}
126+
127+
@Override
128+
public void apply() {
129+
LOGGER.info("The item flies and strikes the enemies finally returning to owner's hand.");
130+
}
131+
132+
@Override
133+
public void onDeactivate() {
134+
LOGGER.info("The item's glow fades.");
135+
}
136+
}
137+
138+
public class SoulEatingEnchantment implements Enchantment {
139+
140+
@Override
141+
public void onActivate() {
142+
LOGGER.info("The item spreads bloodlust.");
143+
}
144+
145+
@Override
146+
public void apply() {
147+
LOGGER.info("The item eats the soul of enemies.");
148+
}
149+
150+
@Override
151+
public void onDeactivate() {
152+
LOGGER.info("Bloodlust slowly disappears.");
153+
}
154+
}
155+
```
156+
157+
And both the hierarchies in action
158+
159+
```
160+
Sword enchantedSword = new Sword(new SoulEatingEnchantment());
161+
enchantedSword.wield();
162+
enchantedSword.swing();
163+
enchantedSword.unwield();
164+
// The sword is wielded.
165+
// The item spreads bloodlust.
166+
// The sword is swinged.
167+
// The item eats the soul of enemies.
168+
// The sword is unwielded.
169+
// Bloodlust slowly disappears.
170+
171+
Hammer hammer = new Hammer(new FlyingEnchantment());
172+
hammer.wield();
173+
hammer.swing();
174+
hammer.unwield();
175+
// The hammer is wielded.
176+
// The item begins to glow faintly.
177+
// The hammer is swinged.
178+
// The item flies and strikes the enemies finally returning to owner's hand.
179+
// The hammer is unwielded.
180+
// The item's glow fades.
181+
```
22182

23183
## Applicability
24184
Use the Bridge pattern when

bridge/etc/bridge.png

-52.2 KB
Binary file not shown.

bridge/etc/bridge.ucls

Lines changed: 0 additions & 157 deletions
This file was deleted.

0 commit comments

Comments
 (0)