diff --git a/adapter/README.md b/adapter/README.md
index 4059ea852..6de04a72c 100644
--- a/adapter/README.md
+++ b/adapter/README.md
@@ -19,10 +19,85 @@ Convert the interface of a class into another interface the clients
expect. Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces.
-
+## Explanation
-## General usage of Adapter Pattern:
-+ Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
+Real world example
+
+> Consider that you have some pictures in your memory card and you need to transfer them to your computer. In order to transfer them you need some kind of adapter that is compatible with your computer ports so that you can attach memory card to your computer. In this case card reader is an adapter.
+> Another example would be the famous power adapter; a three legged plug can't be connected to a two pronged outlet, it needs to use a power adapter that makes it compatible with the two pronged outlet.
+> Yet another example would be a translator translating words spoken by one person to another
+
+In plain words
+
+> Adapter pattern lets you wrap an otherwise incompatible object in an adapter to make it compatible with another class.
+
+Wikipedia says
+
+> In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.
+
+**Programmatic Example**
+
+Consider a captain that can only use rowing boats and cannot sail at all.
+
+First we have interfaces `RowingBoat` and `FishingBoat`
+
+```
+public interface RowingBoat {
+ void row();
+}
+
+public class FishingBoat {
+ private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoat.class);
+ public void sail() {
+ LOGGER.info("The fishing boat is sailing");
+ }
+}
+```
+
+And captain expects an implementation of `RowingBoat` interface to be able to move
+
+```
+public class Captain implements RowingBoat {
+
+ private RowingBoat rowingBoat;
+
+ public Captain(RowingBoat rowingBoat) {
+ this.rowingBoat = rowingBoat;
+ }
+
+ @Override
+ public void row() {
+ rowingBoat.row();
+ }
+}
+```
+
+Now let's say the pirates are coming and our captain needs to escape but there is only fishing boat available. We need to create an adapter that allows the captain to operate the fishing boat with his rowing boat skills.
+
+```
+public class FishingBoatAdapter implements RowingBoat {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoatAdapter.class);
+
+ private FishingBoat boat;
+
+ public FishingBoatAdapter() {
+ boat = new FishingBoat();
+ }
+
+ @Override
+ public void row() {
+ boat.sail();
+ }
+}
+```
+
+And now the `Captain` can use the `FishingBoat` to escape the pirates.
+
+```
+Captain captain = new Captain(new FishingBoatAdapter());
+captain.row();
+```
## Applicability
Use the Adapter pattern when
@@ -30,6 +105,7 @@ Use the Adapter pattern when
* you want to use an existing class, and its interface does not match the one you need
* you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
+* most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
## Consequences:
Class and object adapters have different trade-offs. A class adapter
diff --git a/adapter/etc/adapter.png b/adapter/etc/adapter.png
deleted file mode 100644
index f43358b04..000000000
Binary files a/adapter/etc/adapter.png and /dev/null differ
diff --git a/adapter/etc/adapter.ucls b/adapter/etc/adapter.ucls
deleted file mode 100644
index 290ff544e..000000000
--- a/adapter/etc/adapter.ucls
+++ /dev/null
@@ -1,70 +0,0 @@
-
-
- * 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} ). * *
* The story of this implementation is this.
- * 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();
}
}
diff --git a/adapter/src/main/java/com/iluwatar/adapter/Captain.java b/adapter/src/main/java/com/iluwatar/adapter/Captain.java
index 6d88b1975..369016980 100644
--- a/adapter/src/main/java/com/iluwatar/adapter/Captain.java
+++ b/adapter/src/main/java/com/iluwatar/adapter/Captain.java
@@ -23,33 +23,26 @@
package com.iluwatar.adapter;
/**
- * The Captain uses {@link BattleShip} to fight.
+ * The Captain uses {@link RowingBoat} to sail.
* 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();
}
}
diff --git a/adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java b/adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java
index 2124d9d9f..c46814d18 100644
--- a/adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java
+++ b/adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java
@@ -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");
}
}
diff --git a/adapter/src/main/java/com/iluwatar/adapter/BattleFishingBoat.java b/adapter/src/main/java/com/iluwatar/adapter/FishingBoatAdapter.java
similarity index 70%
rename from adapter/src/main/java/com/iluwatar/adapter/BattleFishingBoat.java
rename to adapter/src/main/java/com/iluwatar/adapter/FishingBoatAdapter.java
index 5e4da91f8..c94932b2b 100644
--- a/adapter/src/main/java/com/iluwatar/adapter/BattleFishingBoat.java
+++ b/adapter/src/main/java/com/iluwatar/adapter/FishingBoatAdapter.java
@@ -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}).
- * 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();
}
}
diff --git a/adapter/src/main/java/com/iluwatar/adapter/BattleShip.java b/adapter/src/main/java/com/iluwatar/adapter/RowingBoat.java
similarity index 92%
rename from adapter/src/main/java/com/iluwatar/adapter/BattleShip.java
rename to adapter/src/main/java/com/iluwatar/adapter/RowingBoat.java
index 62ee366e7..a9ca9ad39 100644
--- a/adapter/src/main/java/com/iluwatar/adapter/BattleShip.java
+++ b/adapter/src/main/java/com/iluwatar/adapter/RowingBoat.java
@@ -24,13 +24,11 @@ package com.iluwatar.adapter;
/**
* The interface expected by the client.
- * 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();
}
diff --git a/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java b/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java
index 89c9bc5d5..9938f0559 100644
--- a/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java
+++ b/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java
@@ -39,9 +39,9 @@ public class AdapterPatternTest {
private Map