Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions adapter/src/main/java/com/iluwatar/adapter/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
14 changes: 7 additions & 7 deletions adapter/src/main/java/com/iluwatar/adapter/Captain.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
* 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();
}

Expand Down
9 changes: 5 additions & 4 deletions adapter/src/main/java/com/iluwatar/adapter/FishingBoat.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@
package com.iluwatar.adapter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.slf4j.LoggerFactory.getLogger;

/**
*
* Device class (adaptee in the pattern). We want to reuse this class.
* 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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

/**
*
* 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 {
Expand All @@ -37,8 +37,7 @@ public FishingBoatAdapter() {
boat = new FishingBoat();
}

@Override
public void row() {
public final void row() {
boat.sail();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.iluwatar.adapter;