Fixed code to follow coding conventions
This commit is contained in:
@ -1,43 +1,38 @@
|
|||||||
package com.iluwatar.adapter;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An adapter helps two incompatible interfaces to work together. This is the
|
* An adapter helps two incompatible interfaces to work together. This is the real world definition
|
||||||
* real world definition for an adapter. Interfaces may be incompatible but the
|
* for an adapter. Interfaces may be incompatible but the inner functionality should suit the need.
|
||||||
* inner functionality should suit the need. The Adapter design pattern allows
|
* The Adapter design pattern allows otherwise incompatible classes to work together by converting
|
||||||
* otherwise incompatible classes to work together by converting the interface
|
* the interface of one class into an interface expected by the clients.
|
||||||
* of one class into an interface expected by the clients.
|
*
|
||||||
*
|
|
||||||
* <p>
|
* <p>
|
||||||
* There are two variations of the Adapter pattern: The class adapter implements
|
* There are two variations of the Adapter pattern: The class adapter implements the adaptee's
|
||||||
* the adaptee's interface whereas the object adapter uses composition to
|
* interface whereas the object adapter uses composition to contain the adaptee in the adapter
|
||||||
* contain the adaptee in the adapter object. This example uses the object
|
* object. This example uses the object adapter approach.
|
||||||
* adapter approach.
|
*
|
||||||
*
|
|
||||||
* <p>
|
* <p>
|
||||||
* The Adapter ({@link BattleFishingBoat}) converts the interface of the adaptee
|
* The Adapter ({@link BattleFishingBoat}) converts the interface of the adaptee class (
|
||||||
* class ( {@link FishingBoat}) into a suitable one expected by the client (
|
* {@link FishingBoat}) into a suitable one expected by the client ( {@link BattleShip} ).
|
||||||
* {@link BattleShip} ).
|
*
|
||||||
*
|
|
||||||
* <p>
|
* <p>
|
||||||
* The story of this implementation is this. <br>
|
* The story of this implementation is this. <br>
|
||||||
* Pirates are coming! we need a {@link BattleShip} to fight! We have a
|
* Pirates are coming! we need a {@link BattleShip} to fight! We have a {@link FishingBoat} and our
|
||||||
* {@link FishingBoat} and our captain. We have no time to make up a new ship!
|
* captain. We have no time to make up a new ship! we need to reuse this {@link FishingBoat}. The
|
||||||
* we need to reuse this {@link FishingBoat}. The captain needs a battleship
|
* captain needs a battleship which can fire and move. The spec is in {@link BattleShip}. We will
|
||||||
* which can fire and move. The spec is in {@link BattleShip}. We will use the
|
* use the Adapter pattern to reuse {@link FishingBoat}.
|
||||||
* Adapter pattern to reuse {@link FishingBoat}.
|
*
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point.
|
* Program entry point.
|
||||||
*
|
*
|
||||||
* @param args
|
* @param args command line args
|
||||||
* command line args
|
*/
|
||||||
*/
|
public static void main(String[] args) {
|
||||||
public static void main(String[] args) {
|
Captain captain = new Captain(new BattleFishingBoat());
|
||||||
Captain captain = new Captain(new BattleFishingBoat());
|
captain.move();
|
||||||
captain.move();
|
captain.fire();
|
||||||
captain.fire();
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package com.iluwatar.adapter;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link BattleShip}
|
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link BattleShip}
|
||||||
* interface expected by the client ({@link Captain}). <br>
|
* 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
|
* 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
|
* {@link FishingBoat} without changing itself. The Adapter class can just map the functions of the
|
||||||
* Adaptee or add, delete features of the Adaptee.
|
* Adaptee or add, delete features of the Adaptee.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class BattleFishingBoat implements BattleShip {
|
public class BattleFishingBoat implements BattleShip {
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ package com.iluwatar.adapter;
|
|||||||
/**
|
/**
|
||||||
* The interface expected by the client.<br>
|
* The interface expected by the client.<br>
|
||||||
* A Battleship can fire and move.
|
* A Battleship can fire and move.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public interface BattleShip {
|
public interface BattleShip {
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package com.iluwatar.adapter;
|
package com.iluwatar.adapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Device class (adaptee in the pattern). We want to reuse this class
|
* Device class (adaptee in the pattern). We want to reuse this class
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class FishingBoat {
|
public class FishingBoat {
|
||||||
|
|
||||||
|
@ -15,77 +15,52 @@ import com.iluwatar.adapter.Captain;
|
|||||||
import com.iluwatar.adapter.FishingBoat;
|
import com.iluwatar.adapter.FishingBoat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An adapter helps two incompatible interfaces to work together. This is the
|
* Test class
|
||||||
* real world definition for an adapter. Interfaces may be incompatible but the
|
|
||||||
* inner functionality should suit the need. The Adapter design pattern allows
|
|
||||||
* otherwise incompatible classes to work together by converting the interface
|
|
||||||
* of one class into an interface expected by the clients.
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* There are two variations of the Adapter pattern: The class adapter implements
|
|
||||||
* the adaptee's interface whereas the object adapter uses composition to
|
|
||||||
* contain the adaptee in the 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} ).
|
|
||||||
*
|
|
||||||
* <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 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 use the
|
|
||||||
* Adapter pattern to reuse {@link FishingBoat} which operates properly
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class AdapterPatternTest {
|
public class AdapterPatternTest {
|
||||||
|
|
||||||
private Map<String, Object> beans;
|
private Map<String, Object> beans;
|
||||||
|
|
||||||
private static final String BATTLESHIP_BEAN = "engineer";
|
private static final String BATTLESHIP_BEAN = "engineer";
|
||||||
|
|
||||||
private static final String CAPTAIN_BEAN = "captain";
|
private static final String CAPTAIN_BEAN = "captain";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method runs before the test execution and sets the bean objects in
|
* This method runs before the test execution and sets the bean objects in the beans Map.
|
||||||
* the beans Map.
|
*/
|
||||||
*/
|
@Before
|
||||||
@Before
|
public void setup() {
|
||||||
public void setup() {
|
beans = new HashMap<>();
|
||||||
beans = new HashMap<>();
|
|
||||||
|
|
||||||
BattleFishingBoat battleFishingBoat = spy(new BattleFishingBoat());
|
BattleFishingBoat battleFishingBoat = spy(new BattleFishingBoat());
|
||||||
beans.put(BATTLESHIP_BEAN, battleFishingBoat);
|
beans.put(BATTLESHIP_BEAN, battleFishingBoat);
|
||||||
|
|
||||||
Captain captain = new Captain();
|
Captain captain = new Captain();
|
||||||
captain.setBattleship((BattleFishingBoat) beans.get(BATTLESHIP_BEAN));
|
captain.setBattleship((BattleFishingBoat) beans.get(BATTLESHIP_BEAN));
|
||||||
beans.put(CAPTAIN_BEAN, captain);
|
beans.put(CAPTAIN_BEAN, captain);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test asserts that when we use the move() method on a captain
|
* This test asserts that when we use the move() method on a captain bean(client), it is
|
||||||
* bean(client), it is internally calling move method on the battleship
|
* internally calling move method on the battleship object. The Adapter ({@link BattleFishingBoat}
|
||||||
* object. The Adapter ({@link BattleFishingBoat}) converts the interface of
|
* ) converts the interface of the target class ( {@link FishingBoat}) into a suitable one
|
||||||
* the target class ( {@link FishingBoat}) into a suitable one expected by
|
* expected by the client ({@link Captain} ).
|
||||||
* the client ({@link Captain} ).
|
*/
|
||||||
*/
|
@Test
|
||||||
@Test
|
public void testAdapter() {
|
||||||
public void testAdapter() {
|
BattleShip captain = (BattleShip) beans.get(CAPTAIN_BEAN);
|
||||||
BattleShip captain = (BattleShip) beans.get(CAPTAIN_BEAN);
|
|
||||||
|
|
||||||
// when captain moves
|
// when captain moves
|
||||||
captain.move();
|
captain.move();
|
||||||
|
|
||||||
// the captain internally calls the battleship object to move
|
// the captain internally calls the battleship object to move
|
||||||
BattleShip battleship = (BattleShip) beans.get(BATTLESHIP_BEAN);
|
BattleShip battleship = (BattleShip) beans.get(BATTLESHIP_BEAN);
|
||||||
verify(battleship).move();
|
verify(battleship).move();
|
||||||
|
|
||||||
// same with above with firing
|
// same with above with firing
|
||||||
captain.fire();
|
captain.fire();
|
||||||
verify(battleship).fire();
|
verify(battleship).fire();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user