https://github.com/iluwatar/java-design-patterns/issues/1016 - decrease number of checkstyle errors in adapter pattern (#1033)

This commit is contained in:
adamski.pro 2019-10-26 07:34:30 +02:00 committed by Ilkka Seppälä
parent 1cb1bdcb57
commit 6aeafcf2aa
5 changed files with 39 additions and 31 deletions

View File

@ -24,37 +24,44 @@
package com.iluwatar.adapter; package com.iluwatar.adapter;
/** /**
* An adapter helps two incompatible interfaces to work together. This is the real world definition * An adapter helps two incompatible interfaces to work together. This is the
* for an adapter. Interfaces may be incompatible but the inner functionality should suit the need. * real world definition for an adapter. Interfaces may be incompatible but
* The Adapter design pattern allows otherwise incompatible classes to work together by converting * the inner functionality should suit the need. The Adapter design pattern
* the interface of one class into an interface expected by the clients. * allows otherwise incompatible classes to work together by converting the
* interface of one class into an interface expected by the clients.
* *
* <p> * <p>
* There are two variations of the Adapter pattern: The class adapter implements the adaptee's * There are two variations of the Adapter pattern: The class adapter
* interface whereas the object adapter uses composition to contain the adaptee in the adapter * implements the adaptee's interface whereas the object adapter uses
* object. This example uses the object adapter approach. * composition to contain the adaptee in the adapter object. This example uses
* the object adapter approach.
* *
* <p> * <p>
* The Adapter ({@link FishingBoatAdapter}) converts the interface of the adaptee class ( * The Adapter ({@link FishingBoatAdapter}) converts the interface of the
* {@link FishingBoat}) into a suitable one expected by the client ( {@link RowingBoat} ). * adaptee class ({@link FishingBoat}) into a suitable one expected by the
* client ({@link RowingBoat}).
* *
* <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 RowingBoat} to flee! We have a {@link FishingBoat} and our * Pirates are coming! we need a {@link RowingBoat} to flee! We have a
* captain. We have no time to make up a new ship! we need to reuse this {@link FishingBoat}. The * {@link FishingBoat} and our captain. We have no time to make up a new ship!
* captain needs a rowing boat which he can operate. The spec is in {@link RowingBoat}. We will * we need to reuse this {@link FishingBoat}. The captain needs a rowing boat
* use the Adapter pattern to reuse {@link FishingBoat}. * which he can operate. The spec is in {@link RowingBoat}. We will use the
* Adapter pattern to reuse {@link FishingBoat}.
* *
*/ */
public class App { public final class App {
private App() { }
/** /**
* Program entry point. * Program entry point.
* *
* @param args command line args * @param args command line args
*/ */
public static void main(String[] args) { public static void main(final String[] args) {
// The captain can only operate rowing boats but with adapter he is able to use fishing boats as well // The captain can only operate rowing boats but with adapter he is able to
// use fishing boats as well
var captain = new Captain(new FishingBoatAdapter()); var captain = new Captain(new FishingBoatAdapter());
captain.row(); captain.row();
} }

View File

@ -27,21 +27,21 @@ package com.iluwatar.adapter;
* The Captain uses {@link RowingBoat} to sail. <br> * The Captain uses {@link RowingBoat} to sail. <br>
* This is the client in the pattern. * This is the client in the pattern.
*/ */
public class Captain { public final class Captain {
private RowingBoat rowingBoat; private RowingBoat rowingBoat;
public Captain() {} public Captain() { }
public Captain(RowingBoat rowingBoat) { public Captain(final RowingBoat boat) {
this.rowingBoat = rowingBoat; this.rowingBoat = boat;
} }
public void setRowingBoat(RowingBoat rowingBoat) { void setRowingBoat(final RowingBoat boat) {
this.rowingBoat = rowingBoat; this.rowingBoat = boat;
} }
public void row() { void row() {
rowingBoat.row(); rowingBoat.row();
} }

View File

@ -24,7 +24,8 @@
package com.iluwatar.adapter; package com.iluwatar.adapter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.slf4j.LoggerFactory.getLogger;
/** /**
* *
@ -32,11 +33,11 @@ import org.slf4j.LoggerFactory;
* Fishing boat moves by sailing. * Fishing boat moves by sailing.
* *
*/ */
public class FishingBoat { final class FishingBoat {
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoat.class); private static final Logger LOGGER = getLogger(FishingBoat.class);
public void sail() { void sail() {
LOGGER.info("The fishing boat is sailing"); LOGGER.info("The fishing boat is sailing");
} }

View File

@ -25,8 +25,8 @@ package com.iluwatar.adapter;
/** /**
* *
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link RowingBoat} * Adapter class. Adapts the interface of the device ({@link FishingBoat})
* interface expected by the client ({@link Captain}). * into {@link RowingBoat} interface expected by the client ({@link Captain}).
* *
*/ */
public class FishingBoatAdapter implements RowingBoat { public class FishingBoatAdapter implements RowingBoat {
@ -37,8 +37,7 @@ public class FishingBoatAdapter implements RowingBoat {
boat = new FishingBoat(); boat = new FishingBoat();
} }
@Override public final void row() {
public void row() {
boat.sail(); boat.sail();
} }
} }

View File

@ -0,0 +1 @@
package com.iluwatar.adapter;