diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/AdapterPatternDriver.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/AdapterPatternDriver.java index 0d69d0e7ec..9d4fcf3574 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/adapter/AdapterPatternDriver.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/AdapterPatternDriver.java @@ -5,9 +5,16 @@ import static com.baeldung.designpatterns.util.LogerUtil.LOG; public class AdapterPatternDriver { public static void main(String args[]) { - LuxuryCarsSpeedAdapter luxuryCars = new LuxuryCarsSpeedAdapterImpl(); - LOG.info("Bugatti Veyron Super Sport's top speed is " + luxuryCars.bugattiVeyronInKMPH() + " Kmph."); - LOG.info("McLaren F1 top speed is " + luxuryCars.mcLarenInKMPH() + " Kmph."); - LOG.info("Aston Martin One-77 top speed is " + luxuryCars.astonMartinInKMPH() + " Kmph."); + LuxuryCars bugattiVeyron = new BugattiVeyron(); + LuxuryCarsAdapter bugattiVeyronAdapter = new LuxuryCarsAdapterImpl(bugattiVeyron); + LOG.info("Bugatti Veyron Super Sport's top speed is " + bugattiVeyronAdapter.speedInKMPH() + " Kmph."); + + LuxuryCars mcLaren = new McLaren(); + LuxuryCarsAdapter mcLarenAdapter = new LuxuryCarsAdapterImpl(mcLaren); + LOG.info("McLaren F1 top speed is " + mcLarenAdapter.speedInKMPH() + " Kmph."); + + LuxuryCars astonMartin = new AstonMartin(); + LuxuryCarsAdapter astonMartinAdapter = new LuxuryCarsAdapterImpl(astonMartin); + LOG.info("McLaren F1 top speed is " + astonMartinAdapter.speedInKMPH() + " Kmph."); } } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/AstonMartin.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/AstonMartin.java new file mode 100644 index 0000000000..b1fba0dae3 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/AstonMartin.java @@ -0,0 +1,8 @@ +package com.baeldung.designpatterns.adapter; + +public class AstonMartin implements LuxuryCars { + @Override + public double speedInMPH() { + return 220; + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/BugattiVeyron.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/BugattiVeyron.java new file mode 100644 index 0000000000..1b0b834448 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/BugattiVeyron.java @@ -0,0 +1,8 @@ +package com.baeldung.designpatterns.adapter; + +public class BugattiVeyron implements LuxuryCars { + @Override + public double speedInMPH() { + return 268; + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCars.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCars.java new file mode 100644 index 0000000000..9926f5f8bc --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCars.java @@ -0,0 +1,5 @@ +package com.baeldung.designpatterns.adapter; + +public interface LuxuryCars { + public double speedInMPH(); +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapter.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapter.java new file mode 100644 index 0000000000..f945e1b389 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapter.java @@ -0,0 +1,5 @@ +package com.baeldung.designpatterns.adapter; + +public interface LuxuryCarsAdapter { + public double speedInKMPH(); +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapterImpl.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapterImpl.java new file mode 100644 index 0000000000..f2bf553292 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapterImpl.java @@ -0,0 +1,19 @@ +package com.baeldung.designpatterns.adapter; + +public class LuxuryCarsAdapterImpl implements LuxuryCarsAdapter { + private LuxuryCars luxuryCars; + + public LuxuryCarsAdapterImpl(LuxuryCars luxuryCars) { + this.luxuryCars = luxuryCars; + } + + @Override + public double speedInKMPH() { + double mph = luxuryCars.speedInMPH(); + return convertMPHtoKMPH(mph); + } + + private double convertMPHtoKMPH(double mph) { + return mph * 1.60934; + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsSpeed.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsSpeed.java deleted file mode 100644 index 0b97b8228c..0000000000 --- a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsSpeed.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.designpatterns.adapter; - -public class LuxuryCarsSpeed { - public double bugattiVeyronInMPH() { - return 268; - } - - public double mcLarenInMPH() { - return 241; - } - - public double astonMartinInMPH() { - return 220; - } -} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsSpeedAdapter.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsSpeedAdapter.java deleted file mode 100644 index d9255f0910..0000000000 --- a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsSpeedAdapter.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.designpatterns.adapter; - -public interface LuxuryCarsSpeedAdapter { - public double bugattiVeyronInKMPH(); - - public double mcLarenInKMPH(); - - public double astonMartinInKMPH(); -} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsSpeedAdapterImpl.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsSpeedAdapterImpl.java deleted file mode 100644 index 2767b78e38..0000000000 --- a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsSpeedAdapterImpl.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.designpatterns.adapter; - -public class LuxuryCarsSpeedAdapterImpl extends LuxuryCarsSpeed implements LuxuryCarsSpeedAdapter { - - @Override - public double bugattiVeyronInKMPH() { - double mph = super.bugattiVeyronInMPH(); - return convertMPHtoKMPH(mph); - } - - @Override - public double mcLarenInKMPH() { - double mph = super.mcLarenInMPH(); - return convertMPHtoKMPH(mph); - } - - @Override - public double astonMartinInKMPH() { - double mph = super.astonMartinInMPH(); - return convertMPHtoKMPH(mph); - } - - private double convertMPHtoKMPH(double mph) { - return mph * 1.60934; - } -} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/McLaren.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/McLaren.java new file mode 100644 index 0000000000..e2ba2b830d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/McLaren.java @@ -0,0 +1,8 @@ +package com.baeldung.designpatterns.adapter; + +public class McLaren implements LuxuryCars { + @Override + public double speedInMPH() { + return 241; + } +} diff --git a/core-java/src/test/java/com/baeldung/designpatterns/AdapterPatternIntegrationTest.java b/core-java/src/test/java/com/baeldung/designpatterns/AdapterPatternIntegrationTest.java index fb483a8a68..e56e271743 100644 --- a/core-java/src/test/java/com/baeldung/designpatterns/AdapterPatternIntegrationTest.java +++ b/core-java/src/test/java/com/baeldung/designpatterns/AdapterPatternIntegrationTest.java @@ -1,20 +1,20 @@ package com.baeldung.designpatterns; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import org.junit.Test; -import com.baeldung.designpatterns.adapter.LuxuryCarsSpeedAdapter; -import com.baeldung.designpatterns.adapter.LuxuryCarsSpeedAdapterImpl; +import com.baeldung.designpatterns.adapter.AstonMartin; +import com.baeldung.designpatterns.adapter.BugattiVeyron; +import com.baeldung.designpatterns.adapter.LuxuryCarsAdapterImpl; +import com.baeldung.designpatterns.adapter.McLaren; public class AdapterPatternIntegrationTest { @Test public void givenLuxuryCarsAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() { - LuxuryCarsSpeedAdapter luxuryCars = new LuxuryCarsSpeedAdapterImpl(); - assertEquals(luxuryCars.bugattiVeyronInKMPH(), 431.30312, 0.00001); - assertEquals(luxuryCars.mcLarenInKMPH(), 387.85094, 0.00001); - assertEquals(luxuryCars.astonMartinInKMPH(), 354.0548, 0.00001); + assertEquals(new LuxuryCarsAdapterImpl(new BugattiVeyron()).speedInKMPH(), 431.30312, 0.00001); + assertEquals(new LuxuryCarsAdapterImpl(new McLaren()).speedInKMPH(), 387.85094, 0.00001); + assertEquals(new LuxuryCarsAdapterImpl(new AstonMartin()).speedInKMPH(), 354.0548, 0.00001); } }