Skip to content

Commit

Permalink
issue iluwatar#292
Browse files Browse the repository at this point in the history
  • Loading branch information
JuhoKang committed Dec 1, 2015
1 parent 5bec636 commit 119d264
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 172 deletions.
Binary file modified adapter/etc/adapter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 29 additions & 29 deletions adapter/etc/adapter.ucls
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
<class id="1" language="java" name="com.iluwatar.adapter.GnomeEngineeringManager" project="adapter"
file="/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java" binary="false" corner="BOTTOM_RIGHT">
<position height="106" width="224" x="110" y="210"/>
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
<class id="1" language="java" name="com.iluwatar.adapter.FishingBoat" project="adapter"
file="/adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="656" y="355"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<interface id="2" language="java" name="com.iluwatar.adapter.Engineer" project="adapter"
file="/adapter/src/main/java/com/iluwatar/adapter/Engineer.java" binary="false" corner="BOTTOM_RIGHT">
<position height="88" width="141" x="110" y="356"/>
<class id="2" language="java" name="com.iluwatar.adapter.Captain" project="adapter"
file="/adapter/src/main/java/com/iluwatar/adapter/Captain.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="228" y="185"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<class id="3" language="java" name="com.iluwatar.adapter.GnomeEngineer" project="adapter"
file="/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineer.java" binary="false" corner="BOTTOM_RIGHT">
<position height="106" width="141" x="374" y="210"/>
</class>
<class id="3" language="java" name="com.iluwatar.adapter.BattleFishingBoat" project="adapter"
file="/adapter/src/main/java/com/iluwatar/adapter/BattleFishingBoat.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="463" y="357"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="4" language="java" name="com.iluwatar.adapter.GoblinGlider" project="adapter"
file="/adapter/src/main/java/com/iluwatar/adapter/GoblinGlider.java" binary="false" corner="BOTTOM_RIGHT">
<position height="142" width="130" x="374" y="356"/>
<interface id="4" language="java" name="com.iluwatar.adapter.BattleShip" project="adapter"
file="/adapter/src/main/java/com/iluwatar/adapter/BattleShip.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="466" y="170"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
</interface>
<realization id="5">
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="2"/>
</realization>
<realization id="6">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="2"/>
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="4"/>
</realization>
<association id="7">
<end type="SOURCE" refId="1" navigable="false">
<attribute id="8" name="engineer"/>
<multiplicity id="9" minimum="0" maximum="1"/>
<association id="6">
<end type="SOURCE" refId="3" navigable="false">
<attribute id="7" name="boat"/>
<multiplicity id="8" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="2" navigable="true"/>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<realization id="9">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="4"/>
</realization>
<association id="10">
<end type="SOURCE" refId="3" navigable="false">
<attribute id="11" name="glider"/>
<end type="SOURCE" refId="2" navigable="false">
<attribute id="11" name="battleship"/>
<multiplicity id="12" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="4" navigable="true"/>
Expand Down
Binary file removed adapter/etc/adapter_1.png
Binary file not shown.
2 changes: 1 addition & 1 deletion adapter/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tags:
expect. Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces.

![alt text](./etc/adapter_1.png "Adapter")
![alt text](./etc/adapter.png "Adapter")

**Applicability:** Use the Adapter pattern when

Expand Down
53 changes: 34 additions & 19 deletions adapter/src/main/java/com/iluwatar/adapter/App.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
package com.iluwatar.adapter;

/**
* An adapter helps two incompatible interfaces to work together. This is the 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.
* An adapter helps two incompatible interfaces to work together. This is the
* 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>
* 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}.
*
* <p>The Adapter ({@link GnomeEngineer}) converts the interface of the target class (
* {@link GoblinGlider}) into a suitable one expected by the client ({@link GnomeEngineeringManager}
* ).
*/
public class App {

/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
Engineer manager = new GnomeEngineeringManager(new GnomeEngineer());
manager.operateDevice();
}
/**
* Program entry point.
*
* @param args
* command line args
*/
public static void main(String[] args) {
Captain captain = new Captain(new BattleFishingBoat());
captain.move();
captain.fire();
}
}
11 changes: 0 additions & 11 deletions adapter/src/main/java/com/iluwatar/adapter/Engineer.java

This file was deleted.

23 changes: 0 additions & 23 deletions adapter/src/main/java/com/iluwatar/adapter/GnomeEngineer.java

This file was deleted.

This file was deleted.

21 changes: 0 additions & 21 deletions adapter/src/main/java/com/iluwatar/adapter/GoblinGlider.java

This file was deleted.

107 changes: 65 additions & 42 deletions adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,83 @@
import org.junit.Before;
import org.junit.Test;

import com.iluwatar.adapter.BattleFishingBoat;
import com.iluwatar.adapter.BattleShip;
import com.iluwatar.adapter.Captain;
import com.iluwatar.adapter.FishingBoat;

/**
* An adapter helps two incompatible interfaces to work together. This is the 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.
* An adapter helps two incompatible interfaces to work together. This is the
* 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>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 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
*
* <p>The Adapter ({@link GnomeEngineer}) converts the interface
* of the target class ({@link GoblinGlider}) into a suitable one expected by
* the client ({@link GnomeEngineeringManager}
* ).
*/
public class AdapterPatternTest {

private Map<String, Object> beans;
private Map<String, Object> beans;

private static final String BATTLESHIP_BEAN = "engineer";

private static final String CAPTAIN_BEAN = "captain";

private static final String ENGINEER_BEAN = "engineer";
/**
* This method runs before the test execution and sets the bean objects in
* the beans Map.
*/
@Before
public void setup() {
beans = new HashMap<>();

private static final String MANAGER_BEAN = "manager";
BattleFishingBoat battleFishingBoat = spy(new BattleFishingBoat());
beans.put(BATTLESHIP_BEAN, battleFishingBoat);

/**
* This method runs before the test execution and sets the bean objects in the beans Map.
*/
@Before
public void setup() {
beans = new HashMap<>();
Captain captain = new Captain();
captain.setBattleship((BattleFishingBoat) beans.get(BATTLESHIP_BEAN));
beans.put(CAPTAIN_BEAN, captain);
}

GnomeEngineer gnomeEngineer = spy(new GnomeEngineer());
beans.put(ENGINEER_BEAN, gnomeEngineer);
/**
* 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}) 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);

GnomeEngineeringManager manager = new GnomeEngineeringManager();
manager.setEngineer((GnomeEngineer) beans.get(ENGINEER_BEAN));
beans.put(MANAGER_BEAN, manager);
}
// when captain moves
captain.move();

/**
* This test asserts that when we call operateDevice() method on a manager bean, it is internally
* calling operateDevice method on the engineer object. The Adapter ({@link GnomeEngineer})
* converts the interface of the target class ( {@link GoblinGlider}) into a suitable one expected
* by the client ({@link GnomeEngineeringManager} ).
*/
@Test
public void testAdapter() {
Engineer manager = (Engineer) beans.get(MANAGER_BEAN);
// the captain internally calls the battleship object to move
BattleShip battleship = (BattleShip) beans.get(BATTLESHIP_BEAN);
verify(battleship).move();

// when manager is asked to operate device
manager.operateDevice();
// same with above with firing
captain.fire();
verify(battleship).fire();

// Manager internally calls the engineer object to operateDevice
Engineer engineer = (Engineer) beans.get(ENGINEER_BEAN);
verify(engineer).operateDevice();
}
}
}

0 comments on commit 119d264

Please sign in to comment.