https://github.com/iluwatar/java-design-patterns/issues/1016 - decrease number of checkstyle errors in adapter pattern (#1033)
This commit is contained in:
parent
1cb1bdcb57
commit
6aeafcf2aa
@ -24,37 +24,44 @@
|
||||
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.
|
||||
* 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 FishingBoatAdapter}) converts the interface of the adaptee class (
|
||||
* {@link FishingBoat}) into a suitable one expected by the client ( {@link RowingBoat} ).
|
||||
* 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 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 rowing boat which he can operate. The spec is in {@link RowingBoat}. We will
|
||||
* use the Adapter pattern to reuse {@link FishingBoat}.
|
||||
* 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 rowing boat
|
||||
* 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.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// The captain can only operate rowing boats but with adapter he is able to use fishing boats as well
|
||||
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
|
||||
var captain = new Captain(new FishingBoatAdapter());
|
||||
captain.row();
|
||||
}
|
||||
|
@ -27,21 +27,21 @@ package com.iluwatar.adapter;
|
||||
* The Captain uses {@link RowingBoat} to sail. <br>
|
||||
* This is the client in the pattern.
|
||||
*/
|
||||
public class Captain {
|
||||
public final class Captain {
|
||||
|
||||
private RowingBoat rowingBoat;
|
||||
|
||||
public Captain() {}
|
||||
public Captain() { }
|
||||
|
||||
public Captain(RowingBoat rowingBoat) {
|
||||
this.rowingBoat = rowingBoat;
|
||||
public Captain(final RowingBoat boat) {
|
||||
this.rowingBoat = boat;
|
||||
}
|
||||
|
||||
public void setRowingBoat(RowingBoat rowingBoat) {
|
||||
this.rowingBoat = rowingBoat;
|
||||
void setRowingBoat(final RowingBoat boat) {
|
||||
this.rowingBoat = boat;
|
||||
}
|
||||
|
||||
public void row() {
|
||||
void row() {
|
||||
rowingBoat.row();
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,8 @@
|
||||
package com.iluwatar.adapter;
|
||||
|
||||
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.
|
||||
*
|
||||
*/
|
||||
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");
|
||||
}
|
||||
|
||||
|
@ -25,8 +25,8 @@ package com.iluwatar.adapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* Adapter class. Adapts the interface of the device ({@link FishingBoat}) into {@link RowingBoat}
|
||||
* interface expected by the client ({@link Captain}).
|
||||
* Adapter class. Adapts the interface of the device ({@link FishingBoat})
|
||||
* into {@link RowingBoat} interface expected by the client ({@link Captain}).
|
||||
*
|
||||
*/
|
||||
public class FishingBoatAdapter implements RowingBoat {
|
||||
@ -37,8 +37,7 @@ public class FishingBoatAdapter implements RowingBoat {
|
||||
boat = new FishingBoat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void row() {
|
||||
public final void row() {
|
||||
boat.sail();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1 @@
|
||||
package com.iluwatar.adapter;
|
Loading…
x
Reference in New Issue
Block a user