File tree Expand file tree Collapse file tree 4 files changed +94
-0
lines changed
adapter/src/main/java/com/iluwatar/adapter Expand file tree Collapse file tree 4 files changed +94
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments