8000 Work on improved Bridge example · sithuhlaing/java-design-patterns@a5ec376 · GitHub
[go: up one dir, main page]

Skip to content

Commit a5ec376

Browse files
committed
Work on improved Bridge example
1 parent ed1a002 commit a5ec376

20 files changed

+129
-612
lines changed

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.

bridge/etc/bridge.urm.puml

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

bridge/src/main/java/com/iluwatar/bridge/App.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,42 @@
2222
*/
2323
package com.iluwatar.bridge;
2424

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
2528
/**
2629
*
27-
* The Bridge pattern can also be thought of as two layers of abstraction. With Bridge, you can
28-
* decouple an abstraction from its implementation so that the two can vary independently.
30+
* Composition over inheritance. The Bridge pattern can also be thought of as two layers of abstraction.
31+
* With Bridge, you can decouple an abstraction from its implementation so that the two can vary independently.
2932
* <p>
30-
* In Bridge pattern both abstraction ({@link MagicWeapon}) and implementation (
31-
* {@link MagicWeaponImpl}) have their own class hierarchies. The interface of the implementations
33+
* In Bridge pattern both abstraction ({@link Weapon}) and implementation (
34+
* {@link Enchantment}) have their own class hierarchies. The interface of the implementations
3235
* can be changed without affecting the clients.
36+
* <p>
37+
* In this example we have two class hierarchies. One of weapons and another one of enchantments. We can easily
38+
* combine any weapon with any enchantment using composition instead of creating deep class hierarchy.
3339
*
3440
*/
3541
public class App {
3642

43+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
44+
3745
/**
3846
* Program entry point
3947
*
4048
* @param args command line args
4149
*/
4250
public static void main(String[] args) {
43-
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(new Excalibur());
44-
blindingMagicWeapon.wield();
45-
blindingMagicWeapon.blind();
46-
blindingMagicWeapon.swing();
47-
blindingMagicWeapon.unwield();
48-
49-
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(new Mjollnir());
50-
flyingMagicWeapon.wield();
51-
flyingMagicWeapon.fly();
52-
flyingMagicWeapon.swing();
53-
flyingMagicWeapon.unwield();
51+
LOGGER.info("The knight receives an enchanted sword.");
52+
Sword enchantedSword = new Sword(new SoulEatingEnchantment());
53+
enchantedSword.wield();
54+
enchantedSword.swing();
55+
enchantedSword.unwield();
5456

55-
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(new Stormbringer());
56-
soulEatingMagicWeapon.wield();
57-
soulEatingMagicWeapon.swing();
58-
soulEatingMagicWeapon.eatSoul();
59-
soulEatingMagicWeapon.unwield();
57+
LOGGER.info("The valkyrie receives an enchanted hammer.");
58+
Hammer hammer = new Hammer(new FlyingEnchantment());
59+
hammer.wield();
60+
hammer.swing();
61+
hammer.unwield();
6062
}
6163
}

bridge/src/main/java/com/iluwatar/bridge/BlindingMagicWeapon.java

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

bridge/src/main/java/com/iluwatar/bridge/FlyingMagicWeaponImpl.java renamed to bridge/src/main/java/com/iluwatar/bridge/Enchantment.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424

2525
/**
2626
*
27-
* FlyingMagicWeaponImpl
28-
*
27+
* Enchantment
28+
*
2929
*/
30-
public abstract class FlyingMagicWeaponImpl extends MagicWeaponImpl {
30+
public interface Enchantment {
31+
32+
void onActivate();
3133

32-
public abstract void flyImp();
34+
void apply();
3335

36+
void onDeactivate();
3437
}

0 commit comments

Comments
 (0)
0