#590 Add explanation for Adapter
This commit is contained in:
@ -34,14 +34,14 @@ package com.iluwatar.adapter;
|
||||
* object. This example uses the object adapter approach.
|
||||
*
|
||||
* <p>
|
||||
* The Adapter ({@link BattleFishingBoat}) converts the interface of the adaptee class (
|
||||
* {@link FishingBoat}) into a suitable one expected by the client ( {@link BattleShip} ).
|
||||
* The Adapter ({@link FishingBoatAdapter}) converts the interface of the adaptee class (
|
||||
* {@link FishingBoat}) into a suitable one expected by the client ( {@link RowingBoat} ).
|
||||
*
|
||||
* <p>
|
||||
* The story of this implementation is this. <br>
|
||||
* Pirates are coming! we need a {@link BattleShip} to fight! We have a {@link FishingBoat} and our
|
||||
* Pirates are coming! we need a {@link RowingBoat} to flee! We have a {@link FishingBoat} and our
|
||||
* captain. We have no time to make up a new ship! we need to reuse this {@link FishingBoat}. The
|
||||
* captain needs a battleship which can fire and move. The spec is in {@link BattleShip}. We will
|
||||
* captain needs a rowing boat which he can operate. The spec is in {@link RowingBoat}. We will
|
||||
* use the Adapter pattern to reuse {@link FishingBoat}.
|
||||
*
|
||||
*/
|
||||
@ -53,8 +53,8 @@ public class App {
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Captain captain = new Captain(new BattleFishingBoat());
|
||||
captain.move();
|
||||
captain.fire();
|
||||
// The captain can only operate rowing boats but with adapter he is able to use fishing boats as well
|
||||
Captain captain = new Captain(new FishingBoatAdapter());
|
||||
captain.row();
|
||||
}
|
||||
}
|
||||
|
@ -23,33 +23,26 @@
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
* The Captain uses {@link BattleShip} to fight. <br>
|
||||
* The Captain uses {@link RowingBoat} to sail. <br>
|
||||
* This is the client in the pattern.
|
||||
*/
|
||||
public class Captain implements BattleShip {
|
||||
public class Captain implements RowingBoat {
|
||||
|
||||
private BattleShip battleship;
|
||||
private RowingBoat rowingBoat;
|
||||
|
||||
public Captain() {
|
||||
public Captain() {}
|
||||
|
||||
public Captain(RowingBoat rowingBoat) {
|
||||
this.rowingBoat = rowingBoat;
|
||||
}
|
||||
|
||||
public Captain(BattleShip battleship) {
|
||||
this.battleship = battleship;
|
||||
}
|
||||
|
||||
public void setBattleship(BattleShip battleship) {
|
||||
this.battleship = battleship;
|
||||
public void setRowingBoat(RowingBoat rowingBoat) {
|
||||
this.rowingBoat = rowingBoat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fire() {
|
||||
battleship.fire();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move() {
|
||||
battleship.move();
|
||||
public void row() {
|
||||
rowingBoat.row();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,7 +27,8 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Device class (adaptee in the pattern). We want to reuse this class
|
||||
* Device class (adaptee in the pattern). We want to reuse this class.
|
||||
* Fishing boat moves by sailing.
|
||||
*
|
||||
*/
|
||||
public class FishingBoat {
|
||||
@ -35,11 +36,7 @@ public class FishingBoat {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoat.class);
|
||||
|
||||
public void sail() {
|
||||
LOGGER.info("The Boat is moving to that place");
|
||||
}
|
||||
|
||||
public void fish() {
|
||||
LOGGER.info("fishing ...");
|
||||
LOGGER.info("The fishing boat is sailing");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,30 +27,22 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link BattleShip}
|
||||
* interface expected by the client ({@link Captain}). <br>
|
||||
* In this case we added a new function fire to suit the interface. We are reusing the
|
||||
* {@link FishingBoat} without changing itself. The Adapter class can just map the functions of the
|
||||
* Adaptee or add, delete features of the Adaptee.
|
||||
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link RowingBoat}
|
||||
* interface expected by the client ({@link Captain}).
|
||||
*
|
||||
*/
|
||||
public class BattleFishingBoat implements BattleShip {
|
||||
public class FishingBoatAdapter implements RowingBoat {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(BattleFishingBoat.class);
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoatAdapter.class);
|
||||
|
||||
private FishingBoat boat;
|
||||
|
||||
public BattleFishingBoat() {
|
||||
public FishingBoatAdapter() {
|
||||
boat = new FishingBoat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fire() {
|
||||
LOGGER.info("fire!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move() {
|
||||
public void row() {
|
||||
boat.sail();
|
||||
}
|
||||
}
|
@ -24,13 +24,11 @@ package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
* The interface expected by the client.<br>
|
||||
* A Battleship can fire and move.
|
||||
* A rowing boat is rowed to move.
|
||||
*
|
||||
*/
|
||||
public interface BattleShip {
|
||||
public interface RowingBoat {
|
||||
|
||||
void fire();
|
||||
|
||||
void move();
|
||||
void row();
|
||||
|
||||
}
|
@ -39,9 +39,9 @@ public class AdapterPatternTest {
|
||||
|
||||
private Map<String, Object> beans;
|
||||
|
||||
private static final String BATTLESHIP_BEAN = "engineer";
|
||||
private static final String FISHING_BEAN = "fisher";
|
||||
|
||||
private static final String CAPTAIN_BEAN = "captain";
|
||||
private static final String ROWING_BEAN = "captain";
|
||||
|
||||
/**
|
||||
* This method runs before the test execution and sets the bean objects in the beans Map.
|
||||
@ -50,34 +50,29 @@ public class AdapterPatternTest {
|
||||
public void setup() {
|
||||
beans = new HashMap<>();
|
||||
|
||||
BattleFishingBoat battleFishingBoat = spy(new BattleFishingBoat());
|
||||
beans.put(BATTLESHIP_BEAN, battleFishingBoat);
|
||||
FishingBoatAdapter fishingBoatAdapter = spy(new FishingBoatAdapter());
|
||||
beans.put(FISHING_BEAN, fishingBoatAdapter);
|
||||
|
||||
Captain captain = new Captain();
|
||||
captain.setBattleship((BattleFishingBoat) beans.get(BATTLESHIP_BEAN));
|
||||
beans.put(CAPTAIN_BEAN, captain);
|
||||
captain.setRowingBoat((FishingBoatAdapter) beans.get(FISHING_BEAN));
|
||||
beans.put(ROWING_BEAN, captain);
|
||||
}
|
||||
|
||||
/**
|
||||
* This test asserts that when we use the move() method on a captain bean(client), it is
|
||||
* internally calling move method on the battleship object. The Adapter ({@link BattleFishingBoat}
|
||||
* This test asserts that when we use the row() method on a captain bean(client), it is
|
||||
* internally calling sail method on the fishing boat object. The Adapter ({@link FishingBoatAdapter}
|
||||
* ) converts the interface of the target class ( {@link FishingBoat}) into a suitable one
|
||||
* expected by the client ({@link Captain} ).
|
||||
*/
|
||||
@Test
|
||||
public void testAdapter() {
|
||||
BattleShip captain = (BattleShip) beans.get(CAPTAIN_BEAN);
|
||||
RowingBoat captain = (RowingBoat) beans.get(ROWING_BEAN);
|
||||
|
||||
// when captain moves
|
||||
captain.move();
|
||||
captain.row();
|
||||
|
||||
// the captain internally calls the battleship object to move
|
||||
BattleShip battleship = (BattleShip) beans.get(BATTLESHIP_BEAN);
|
||||
verify(battleship).move();
|
||||
|
||||
// same with above with firing
|
||||
captain.fire();
|
||||
verify(battleship).fire();
|
||||
|
||||
RowingBoat adapter = (RowingBoat) beans.get(FISHING_BEAN);
|
||||
verify(adapter).row();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user