|
9 | 9 | import org.junit.Before; |
10 | 10 | import org.junit.Test; |
11 | 11 |
|
| 12 | +import com.iluwatar.adapter.BattleFishingBoat; |
| 13 | +import com.iluwatar.adapter.BattleShip; |
| 14 | +import com.iluwatar.adapter.Captain; |
| 15 | +import com.iluwatar.adapter.FishingBoat; |
| 16 | + |
12 | 17 | /** |
13 | | - * An adapter helps two incompatible interfaces to work together. This is the real world definition |
14 | | - * for an adapter. Interfaces may be incompatible but the inner functionality should suit the need. |
15 | | - * The Adapter design pattern allows otherwise incompatible classes to work together by converting |
16 | | - * the interface of one class into an interface expected by the clients. |
| 18 | + * An adapter helps two incompatible interfaces to work together. This is the |
| 19 | + * real world definition for an adapter. Interfaces may be incompatible but the |
| 20 | + * inner functionality should suit the need. The Adapter design pattern allows |
| 21 | + * otherwise incompatible classes to work together by converting the interface |
| 22 | + * of one class into an interface expected by the clients. |
| 23 | + * |
| 24 | + * <p> |
| 25 | + * There are two variations of the Adapter pattern: The class adapter implements |
| 26 | + * the adaptee's interface whereas the object adapter uses composition to |
| 27 | + * contain the adaptee in the adapter object. This example uses the object |
| 28 | + * adapter approach. |
| 29 | + * |
| 30 | + * <p> |
| 31 | + * The Adapter ({@link BattleFishingBoat}) converts the interface of the adaptee |
| 32 | + * class ( {@link FishingBoat}) into a suitable one expected by the client ( |
| 33 | + * {@link BattleShip} ). |
17 | 34 | * |
18 | | - * <p>There are two variations of the Adapter pattern: |
19 | | - * The class adapter implements the adaptee's |
20 | | - * interface whereas the object adapter uses composition to contain the adaptee in the adapter |
21 | | - * object. This example uses the object adapter approach. |
| 35 | + * <p> |
| 36 | + * The story of this implementation is this. <br> |
| 37 | + * Pirates are coming! we need a {@link BattleShip} to fight! We have a |
| 38 | + * {@link FishingBoat} and our captain. We have no time to make up a new ship! |
| 39 | + * we need to reuse this {@link FishingBoat}. The captain needs a battleship |
| 40 | + * which can fire and move. The spec is in {@link BattleShip}. We will use the |
| 41 | + * Adapter pattern to reuse {@link FishingBoat} which operates properly |
22 | 42 | * |
23 | | - * <p>The Adapter ({@link GnomeEngineer}) converts the interface |
24 | | - * of the target class ({@link GoblinGlider}) into a suitable one expected by |
25 | | - * the client ({@link GnomeEngineeringManager} |
26 | | - * ). |
27 | 43 | */ |
28 | 44 | public class AdapterPatternTest { |
29 | 45 |
|
30 | | - private Map<String, Object> beans; |
| 46 | + private Map<String, Object> beans; |
| 47 | + |
| 48 | + private static final String BATTLESHIP_BEAN = "engineer"; |
| 49 | + |
| 50 | + private static final String CAPTAIN_BEAN = "captain"; |
31 | 51 |
|
32 | | - private static final String ENGINEER_BEAN = "engineer"; |
| 52 | + /** |
| 53 | + * This method runs before the test execution and sets the bean objects in |
| 54 | + * the beans Map. |
| 55 | + */ |
| 56 | + @Before |
| 57 | + public void setup() { |
| 58 | + beans = new HashMap<>(); |
33 | 59 |
|
34 | | - private static final String MANAGER_BEAN = "manager"; |
| 60 | + BattleFishingBoat battleFishingBoat = spy(new BattleFishingBoat()); |
| 61 | + beans.put(BATTLESHIP_BEAN, battleFishingBoat); |
35 | 62 |
|
36 | | - /** |
37 | | - * This method runs before the test execution and sets the bean objects in the beans Map. |
38 | | - */ |
39 | | - @Before |
40 | | - public void setup() { |
41 | | - beans = new HashMap<>(); |
| 63 | + Captain captain = new Captain(); |
| 64 | + captain.setBattleship((BattleFishingBoat) beans.get(BATTLESHIP_BEAN)); |
| 65 | + beans.put(CAPTAIN_BEAN, captain); |
| 66 | + } |
42 | 67 |
|
43 | | - GnomeEngineer gnomeEngineer = spy(new GnomeEngineer()); |
44 | | - beans.put(ENGINEER_BEAN, gnomeEngineer); |
| 68 | + /** |
| 69 | + * This test asserts that when we use the move() method on a captain |
| 70 | + * bean(client), it is internally calling move method on the battleship |
| 71 | + * object. The Adapter ({@link BattleFishingBoat}) converts the interface of |
| 72 | + * the target class ( {@link FishingBoat}) into a suitable one expected by |
| 73 | + * the client ({@link Captain} ). |
| 74 | + */ |
| 75 | + @Test |
| 76 | + public void testAdapter() { |
| 77 | + BattleShip captain = (BattleShip) beans.get(CAPTAIN_BEAN); |
45 | 78 |
|
46 | | - GnomeEngineeringManager manager = new GnomeEngineeringManager(); |
47 | | - manager.setEngineer((GnomeEngineer) beans.get(ENGINEER_BEAN)); |
48 | | - beans.put(MANAGER_BEAN, manager); |
49 | | - } |
| 79 | + // when captain moves |
| 80 | + captain.move(); |
50 | 81 |
|
51 | | - /** |
52 | | - * This test asserts that when we call operateDevice() method on a manager bean, it is internally |
53 | | - * calling operateDevice method on the engineer object. The Adapter ({@link GnomeEngineer}) |
54 | | - * converts the interface of the target class ( {@link GoblinGlider}) into a suitable one expected |
55 | | - * by the client ({@link GnomeEngineeringManager} ). |
56 | | - */ |
57 | | - @Test |
58 | | - public void testAdapter() { |
59 | | - Engineer manager = (Engineer) beans.get(MANAGER_BEAN); |
| 82 | + // the captain internally calls the battleship object to move |
| 83 | + BattleShip battleship = (BattleShip) beans.get(BATTLESHIP_BEAN); |
| 84 | + verify(battleship).move(); |
60 | 85 |
|
61 | | - // when manager is asked to operate device |
62 | | - manager.operateDevice(); |
| 86 | + // same with above with firing |
| 87 | + captain.fire(); |
| 88 | + verify(battleship).fire(); |
63 | 89 |
|
64 | | - // Manager internally calls the engineer object to operateDevice |
65 | | - Engineer engineer = (Engineer) beans.get(ENGINEER_BEAN); |
66 | | - verify(engineer).operateDevice(); |
67 | | - } |
| 90 | + } |
68 | 91 | } |
0 commit comments