|
22 | 22 | */
|
23 | 23 | package com.iluwatar.bridge;
|
24 | 24 |
|
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
25 | 28 | /**
|
26 | 29 | *
|
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. |
29 | 32 | * <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 |
32 | 35 | * 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. |
33 | 39 | *
|
34 | 40 | */
|
35 | 41 | public class App {
|
36 | 42 |
|
| 43 | + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); |
| 44 | + |
37 | 45 | /**
|
38 | 46 | * Program entry point
|
39 | 47 | *
|
40 | 48 | * @param args command line args
|
41 | 49 | */
|
42 | 50 | 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(); |
54 | 56 |
|
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(); |
60 | 62 | }
|
61 | 63 | }
|
0 commit comments