Skip to content

Commit

Permalink
#590 Add explanation for Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed Aug 19, 2017
1 parent f47dc1e commit fbf5ffe
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 176 deletions.
82 changes: 79 additions & 3 deletions adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,93 @@ 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.

![alt text](./etc/adapter.png "Adapter")
## 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

* 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
Expand Down
Binary file removed adapter/etc/adapter.png
Binary file not shown.
70 changes: 0 additions & 70 deletions adapter/etc/adapter.ucls

This file was deleted.

37 changes: 0 additions & 37 deletions adapter/etc/adapter.urm.puml

This file was deleted.

14 changes: 7 additions & 7 deletions adapter/src/main/java/com/iluwatar/adapter/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
* 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}.
*
*/
Expand All @@ -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();
}
}
27 changes: 10 additions & 17 deletions adapter/src/main/java/com/iluwatar/adapter/Captain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

@Override
public void fire() {
battleship.fire();
public void setRowingBoat(RowingBoat rowingBoat) {
this.rowingBoat = rowingBoat;
}

@Override
public void move() {
battleship.move();
public void row() {
rowingBoat.row();
}

}
9 changes: 3 additions & 6 deletions adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,16 @@

/**
*
* 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 {

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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,22 @@

/**
*
* 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@

/**
* 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();

}

0 comments on commit fbf5ffe

Please sign in to comment.