Skip to content

Commit e85308f

Browse files
committed
Merge pull request iluwatar#309 from JuhoKang/adapterdev
Attempt to solve issue iluwatar#292
2 parents 6c1f025 + e2d8079 commit e85308f

File tree

14 files changed

+170
-149
lines changed

14 files changed

+170
-149
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

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@
55
* for an adapter. Interfaces may be incompatible but the inner functionality should suit the need.
66
* The Adapter design pattern allows otherwise incompatible classes to work together by converting
77
* the interface of one class into an interface expected by the clients.
8-
*
9-
* <p>There are two variations of the Adapter pattern: The class adapter implements the adaptee's
8+
*
9+
* <p>
10+
* There are two variations of the Adapter pattern: The class adapter implements the adaptee's
1011
* interface whereas the object adapter uses composition to contain the adaptee in the adapter
1112
* object. This example uses the object adapter approach.
12-
*
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-
* ).
13+
*
14+
* <p>
15+
* The Adapter ({@link BattleFishingBoat}) converts the interface of the adaptee class (
16+
* {@link FishingBoat}) into a suitable one expected by the client ( {@link BattleShip} ).
17+
*
18+
* <p>
19+
* The story of this implementation is this. <br>
20+
* Pirates are coming! we need a {@link BattleShip} to fight! We have a {@link FishingBoat} and our
21+
* captain. We have no time to make up a new ship! we need to reuse this {@link FishingBoat}. The
22+
* captain needs a battleship which can fire and move. The spec is in {@link BattleShip}. We will
23+
* use the Adapter pattern to reuse {@link FishingBoat}.
24+
*
1625
*/
1726
public class App {
1827

@@ -22,7 +31,8 @@ public class App {
2231
* @param args command line args
2332
*/
2433
public static void main(String[] args) {
25-
Engineer manager = new GnomeEngineeringManager(new GnomeEngineer());
26-
manager.operateDevice();
34+
Captain captain = new Captain(new BattleFishingBoat());
35+
captain.move();
36+
captain.fire();
2737
}
2838
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.iluwatar.adapter;
2+
3+
/**
4+
*
5+
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link BattleShip}
6+
* interface expected by the client ({@link Captain}). <br>
7+
* In this case we added a new function fire to suit the interface. We are reusing the
8+
* {@link FishingBoat} without changing itself. The Adapter class can just map the functions of the
9+
* Adaptee or add, delete features of the Adaptee.
10+
*
11+
*/
12+
public class BattleFishingBoat implements BattleShip {
13+
14+
private FishingBoat boat;
15+
16+
public BattleFishingBoat() {
17+
boat = new FishingBoat();
18+
}
19+
20+
@Override
21+
public void fire() {
22+
System.out.println("fire!");
23+
}
24+
25+
@Override
26+
public void move() {
27+
boat.sail();
28+
}
29+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.iluwatar.adapter;
2+
3+
/**
4+
* The interface expected by the client.<br>
5+
* A Battleship can fire and move.
6+
*
7+
*/
8+
public interface BattleShip {
9+
10+
void fire();
11+
12+
void move();
13+
14+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar.adapter;
2+
3+
/**
4+
* The Captain uses {@link BattleShip} to fight. <br>
5+
* This is the client in the pattern.
6+
*/
7+
public class Captain implements BattleShip {
8+
9+
private BattleShip battleship;
10+
11+
public Captain() {
12+
13+
}
14+
15+
public Captain(BattleShip battleship) {
16+
this.battleship = battleship;
17+
}
18+
19+
public void setBattleship(BattleShip battleship) {
20+
this.battleship = battleship;
21+
}
22+
23+
@Override
24+
public void fire() {
25+
battleship.fire();
26+
}
27+
28+
@Override
29+
public void move() {
30+
battleship.move();
31+
}
32+
33+
}

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

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.iluwatar.adapter;
2+
3+
/**
4+
*
5+
* Device class (adaptee in the pattern). We want to reuse this class
6+
*
7+
*/
8+
public class FishingBoat {
9+
10+
public void sail() {
11+
System.out.println("The Boat is moving to that place");
12+
}
13+
14+
public void fish() {
15+
System.out.println("fishing ...");
16+
}
17+
18+
}

0 commit comments

Comments
 (0)