Skip to content

Commit 119d264

Browse files
committed
1 parent 5bec636 commit 119d264

File tree

10 files changed

+129
-172
lines changed

10 files changed

+129
-172
lines changed

adapter/etc/adapter.png

116 Bytes
Loading

adapter/etc/adapter.ucls

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3-
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
4-
<class id="1" language="java" name="com.iluwatar.adapter.GnomeEngineeringManager" project="adapter"
5-
file="/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java" binary="false" corner="BOTTOM_RIGHT">
6-
<position height="106" width="224" x="110" y="210"/>
2+
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="com.iluwatar.adapter.FishingBoat" project="adapter"
5+
file="/adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="656" y="355"/>
77
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
88
sort-features="false" accessors="true" visibility="true">
99
<attributes public="true" package="true" protected="true" private="true" static="true"/>
1010
<operations public="true" package="true" protected="true" private="true" static="true"/>
1111
</display>
1212
</class>
13-
<interface id="2" language="java" name="com.iluwatar.adapter.Engineer" project="adapter"
14-
file="/adapter/src/main/java/com/iluwatar/adapter/Engineer.java" binary="false" corner="BOTTOM_RIGHT">
15-
<position height="88" width="141" x="110" y="356"/>
13+
<class id="2" language="java" name="com.iluwatar.adapter.Captain" project="adapter"
14+
file="/adapter/src/main/java/com/iluwatar/adapter/Captain.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="228" y="185"/>
1616
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
1717
sort-features="false" accessors="true" visibility="true">
1818
<attributes public="true" package="true" protected="true" private="true" static="true"/>
1919
<operations public="true" package="true" protected="true" private="true" static="true"/>
2020
</display>
21-
</interface>
22-
<class id="3" language="java" name="com.iluwatar.adapter.GnomeEngineer" project="adapter"
23-
file="/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineer.java" binary="false" corner="BOTTOM_RIGHT">
24-
<position height="106" width="141" x="374" y="210"/>
21+
</class>
22+
<class id="3" language="java" name="com.iluwatar.adapter.BattleFishingBoat" project="adapter"
23+
file="/adapter/src/main/java/com/iluwatar/adapter/BattleFishingBoat.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="463" y="357"/>
2525
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
2626
sort-features="false" accessors="true" visibility="true">
2727
<attributes public="true" package="true" protected="true" private="true" static="true"/>
2828
<operations public="true" package="true" protected="true" private="true" static="true"/>
2929
</display>
3030
</class>
31-
<class id="4" language="java" name="com.iluwatar.adapter.GoblinGlider" project="adapter"
32-
file="/adapter/src/main/java/com/iluwatar/adapter/GoblinGlider.java" binary="false" corner="BOTTOM_RIGHT">
33-
<position height="142" width="130" x="374" y="356"/>
31+
<interface id="4" language="java" name="com.iluwatar.adapter.BattleShip" project="adapter"
32+
file="/adapter/src/main/java/com/iluwatar/adapter/BattleShip.java" binary="false" corner="BOTTOM_RIGHT">
33+
<position height="-1" width="-1" x="466" y="170"/>
3434
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
3535
sort-features="false" accessors="true" visibility="true">
3636
<attributes public="true" package="true" protected="true" private="true" static="true"/>
3737
<operations public="true" package="true" protected="true" private="true" static="true"/>
3838
</display>
39-
</class>
39+
</interface>
4040
<realization id="5">
41-
<end type="SOURCE" refId="1"/>
42-
<end type="TARGET" refId="2"/>
43-
</realization>
44-
<realization id="6">
45-
<end type="SOURCE" refId="3"/>
46-
<end type="TARGET" refId="2"/>
41+
<end type="SOURCE" refId="2"/>
42+
<end type="TARGET" refId="4"/>
4743
</realization>
48-
<association id="7">
49-
<end type="SOURCE" refId="1" navigable="false">
50-
<attribute id="8" name="engineer"/>
51-
<multiplicity id="9" minimum="0" maximum="1"/>
44+
<association id="6">
45+
<end type="SOURCE" refId="3" navigable="false">
46+
<attribute id="7" name="boat"/>
47+
<multiplicity id="8" minimum="0" maximum="1"/>
5248
</end>
53-
<end type="TARGET" refId="2" navigable="true"/>
49+
<end type="TARGET" refId="1" navigable="true"/>
5450
<display labels="true" multiplicity="true"/>
5551
</association>
52+
<realization id="9">
53+
<end type="SOURCE" refId="3"/>
54+
<end type="TARGET" refId="4"/>
55+
</realization>
5656
<association id="10">
57-
<end type="SOURCE" refId="3" navigable="false">
58-
<attribute id="11" name="glider"/>
57+
<end type="SOURCE" refId="2" navigable="false">
58+
<attribute id="11" name="battleship"/>
5959
<multiplicity id="12" minimum="0" maximum="1"/>
6060
</end>
6161
<end type="TARGET" refId="4" navigable="true"/>

adapter/etc/adapter_1.png

-29.3 KB
Binary file not shown.

adapter/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ tags:
1515
expect. Adapter lets classes work together that couldn't otherwise because of
1616
incompatible interfaces.
1717

18-
![alt text](./etc/adapter_1.png "Adapter")
18+
![alt text](./etc/adapter.png "Adapter")
1919

2020
**Applicability:** Use the Adapter pattern when
2121

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
package com.iluwatar.adapter;
22

33
/**
4-
* An adapter helps two incompatible interfaces to work together. This is the real world definition
5-
* for an adapter. Interfaces may be incompatible but the inner functionality should suit the need.
6-
* The Adapter design pattern allows otherwise incompatible classes to work together by converting
7-
* the interface of one class into an interface expected by the clients.
4+
* An adapter helps two incompatible interfaces to work together. This is the
5+
* real world definition for an adapter. Interfaces may be incompatible but the
6+
* inner functionality should suit the need. The Adapter design pattern allows
7+
* otherwise incompatible classes to work together by converting the interface
8+
* of one class into an interface expected by the clients.
89
*
9-
* <p>There are two variations of the Adapter pattern: The class adapter implements the adaptee's
10-
* interface whereas the object adapter uses composition to contain the adaptee in the adapter
11-
* object. This example uses the object adapter approach.
10+
* <p>
11+
* There are two variations of the Adapter pattern: The class adapter implements
12+
* the adaptee's interface whereas the object adapter uses composition to
13+
* contain the adaptee in the adapter object. This example uses the object
14+
* adapter approach.
15+
*
16+
* <p>
17+
* The Adapter ({@link BattleFishingBoat}) converts the interface of the adaptee
18+
* class ( {@link FishingBoat}) into a suitable one expected by the client (
19+
* {@link BattleShip} ).
20+
*
21+
* <p>
22+
* The story of this implementation is this. <br>
23+
* Pirates are coming! we need a {@link BattleShip} to fight! We have a
24+
* {@link FishingBoat} and our captain. We have no time to make up a new ship!
25+
* we need to reuse this {@link FishingBoat}. The captain needs a battleship
26+
* which can fire and move. The spec is in {@link BattleShip}. We will use the
27+
* Adapter pattern to reuse {@link FishingBoat}.
1228
*
13-
* <p>The Adapter ({@link GnomeEngineer}) converts the interface of the target class (
14-
* {@link GoblinGlider}) into a suitable one expected by the client ({@link GnomeEngineeringManager}
15-
* ).
1629
*/
1730
public class App {
1831

19-
/**
20-
* Program entry point.
21-
*
22-
* @param args command line args
23-
*/
24-
public static void main(String[] args) {
25-
Engineer manager = new GnomeEngineeringManager(new GnomeEngineer());
26-
manager.operateDevice();
27-
}
32+
/**
33+
* Program entry point.
34+
*
35+
* @param args
36+
* command line args
37+
*/
38+
public static void main(String[] args) {
39+
Captain captain = new Captain(new BattleFishingBoat());
40+
captain.move();
41+
captain.fire();
42+
}
2843
}

adapter/src/main/java/com/iluwatar/adapter/Engineer.java

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

adapter/src/main/java/com/iluwatar/adapter/GnomeEngineer.java

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

adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java

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

adapter/src/main/java/com/iluwatar/adapter/GoblinGlider.java

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

adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java

Lines changed: 65 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,83 @@
99
import org.junit.Before;
1010
import org.junit.Test;
1111

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+
1217
/**
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} ).
1734
*
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
2242
*
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-
* ).
2743
*/
2844
public class AdapterPatternTest {
2945

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";
3151

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<>();
3359

34-
private static final String MANAGER_BEAN = "manager";
60+
BattleFishingBoat battleFishingBoat = spy(new BattleFishingBoat());
61+
beans.put(BATTLESHIP_BEAN, battleFishingBoat);
3562

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+
}
4267

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);
4578

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();
5081

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();
6085

61-
// when manager is asked to operate device
62-
manager.operateDevice();
86+
// same with above with firing
87+
captain.fire();
88+
verify(battleship).fire();
6389

64-
// Manager internally calls the engineer object to operateDevice
65-
Engineer engineer = (Engineer) beans.get(ENGINEER_BEAN);
66-
verify(engineer).operateDevice();
67-
}
90+
}
6891
}

0 commit comments

Comments
 (0)