BAEL-4848 Update "Java Adapter Pattern" article (#13624)

This commit is contained in:
Eugene Kovko 2023-03-13 03:43:43 +01:00 committed by GitHub
parent f0f9e25e83
commit 1db057ebc8
11 changed files with 87 additions and 98 deletions

View File

@ -1,20 +0,0 @@
package com.baeldung.adapter;
import static com.baeldung.util.LoggerUtil.LOG;
public class AdapterPatternDriver {
public static void main(String args[]) {
Movable bugattiVeyron = new BugattiVeyron();
MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron);
LOG.info("Bugatti Veyron Super Sport's top speed is " + bugattiVeyronAdapter.getSpeed() + " Kmph.");
Movable mcLaren = new McLaren();
MovableAdapter mcLarenAdapter = new MovableAdapterImpl(mcLaren);
LOG.info("McLaren F1 top speed is " + mcLarenAdapter.getSpeed() + " Kmph.");
Movable astonMartin = new AstonMartin();
MovableAdapter astonMartinAdapter = new MovableAdapterImpl(astonMartin);
LOG.info("McLaren F1 top speed is " + astonMartinAdapter.getSpeed() + " Kmph.");
}
}

View File

@ -1,8 +0,0 @@
package com.baeldung.adapter;
public class AstonMartin implements Movable {
@Override
public double getSpeed() {
return 220;
}
}

View File

@ -1,8 +0,0 @@
package com.baeldung.adapter;
public class BugattiVeyron implements Movable {
@Override
public double getSpeed() {
return 268;
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.adapter;
import java.util.Enumeration;
import java.util.Iterator;
public class IteratorAdapter<E> implements Iterator<E> {
private Enumeration<E> enumeration;
public IteratorAdapter(Enumeration<E> enumeration) {
this.enumeration = enumeration;
}
@Override
public boolean hasNext() {
return enumeration.hasMoreElements();
}
@Override
public E next() {
return enumeration.nextElement();
}
}

View File

@ -1,8 +0,0 @@
package com.baeldung.adapter;
public class McLaren implements Movable {
@Override
public double getSpeed() {
return 241;
}
}

View File

@ -1,6 +0,0 @@
package com.baeldung.adapter;
public interface Movable {
// returns speed in MPH
double getSpeed();
}

View File

@ -1,6 +0,0 @@
package com.baeldung.adapter;
public interface MovableAdapter {
// returns speed in KMPH
double getSpeed();
}

View File

@ -1,19 +0,0 @@
package com.baeldung.adapter;
public class MovableAdapterImpl implements MovableAdapter {
private Movable luxuryCars;
public MovableAdapterImpl(Movable luxuryCars) {
this.luxuryCars = luxuryCars;
}
@Override
public double getSpeed() {
double mph = luxuryCars.getSpeed();
return convertMPHtoKMPH(mph);
}
private double convertMPHtoKMPH(double mph) {
return mph * 1.60934;
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.adapter;
import java.util.Iterator;
import java.util.StringTokenizer;
public class StringTokenizerIteratorAdapter extends StringTokenizer implements Iterator<String> {
public StringTokenizerIteratorAdapter(final String str, final String delim, final boolean returnDelims) {
super(str, delim, returnDelims);
}
public StringTokenizerIteratorAdapter(final String str, final String delim) {
super(str, delim);
}
public StringTokenizerIteratorAdapter(final String str) {
super(str);
}
@Override
public boolean hasNext() {
return hasMoreTokens();
}
@Override
public String next() {
return nextToken();
}
}

View File

@ -1,23 +0,0 @@
package com.baeldung.adapter;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class AdapterPatternIntegrationTest {
@Test
public void givenMovableAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() {
Movable bugattiVeyron = new BugattiVeyron();
MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron);
assertEquals(bugattiVeyronAdapter.getSpeed(), 431.30312, 0.00001);
Movable mcLaren = new McLaren();
MovableAdapter mcLarenAdapter = new MovableAdapterImpl(mcLaren);
assertEquals(mcLarenAdapter.getSpeed(), 387.85094, 0.00001);
Movable astonMartin = new AstonMartin();
MovableAdapter astonMartinAdapter = new MovableAdapterImpl(astonMartin);
assertEquals(astonMartinAdapter.getSpeed(), 354.0548, 0.00001);
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.adapter;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.junit.jupiter.api.Test;
class IteratorAdapterUnitTest {
public static final String STRING_DATA = "Welcome to baeldung.com";
private final StringTokenizer tokenizer = new StringTokenizer(STRING_DATA);
private final Iterator<String> tokenizerAdapter = new StringTokenizerIteratorAdapter(STRING_DATA);
private final List<String> expectedTokensForString = Arrays.asList("Welcome", "to", "baeldung.com");
@Test
void givenTokenizer_thenAdapterProducesCorrectResult() {
List<String> actualTokens = new ArrayList<>();
new IteratorAdapter(tokenizer).forEachRemaining(s -> actualTokens.add((String) s));
assertEquals(expectedTokensForString, actualTokens);
}
@Test
void givenTokenizerAdapter_thenIteratorProducesCorrectResult() {
List<String> actualTokens = new ArrayList<>();
tokenizerAdapter.forEachRemaining(actualTokens::add);
assertEquals(expectedTokensForString, actualTokens);
}
}