From afe5d241b91874c82a47331688c20db2fc560b70 Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Mon, 27 May 2019 12:05:42 +0530 Subject: [PATCH 1/3] Added adapter pattern and its test case --- .../structural/adapter/BugattiVeyron.java | 8 ++++++++ .../structural/adapter/Movable.java | 6 ++++++ .../structural/adapter/MovableAdapter.java | 16 ++++++++++++++++ .../structural/adapter/MovableAdapterImpl.java | 18 ++++++++++++++++++ .../structural/MovableAdapterTest.java | 18 ++++++++++++++++++ 5 files changed, 66 insertions(+) create mode 100644 src/main/java/com/designpatterns/structural/adapter/BugattiVeyron.java create mode 100644 src/main/java/com/designpatterns/structural/adapter/Movable.java create mode 100644 src/main/java/com/designpatterns/structural/adapter/MovableAdapter.java create mode 100644 src/main/java/com/designpatterns/structural/adapter/MovableAdapterImpl.java create mode 100644 src/test/java/com/designpatterns/structural/MovableAdapterTest.java diff --git a/src/main/java/com/designpatterns/structural/adapter/BugattiVeyron.java b/src/main/java/com/designpatterns/structural/adapter/BugattiVeyron.java new file mode 100644 index 00000000..4cffba43 --- /dev/null +++ b/src/main/java/com/designpatterns/structural/adapter/BugattiVeyron.java @@ -0,0 +1,8 @@ +package src.main.java.com.designpatterns.structural.adapter; + +public class BugattiVeyron implements Movable { + @Override + public double getSpeed() { + return 268; + } +} diff --git a/src/main/java/com/designpatterns/structural/adapter/Movable.java b/src/main/java/com/designpatterns/structural/adapter/Movable.java new file mode 100644 index 00000000..eeca412a --- /dev/null +++ b/src/main/java/com/designpatterns/structural/adapter/Movable.java @@ -0,0 +1,6 @@ +package src.main.java.com.designpatterns.structural.adapter; + +public interface Movable { + // Returns the speed of the movable in MPH + double getSpeed(); +} diff --git a/src/main/java/com/designpatterns/structural/adapter/MovableAdapter.java b/src/main/java/com/designpatterns/structural/adapter/MovableAdapter.java new file mode 100644 index 00000000..3cd11eac --- /dev/null +++ b/src/main/java/com/designpatterns/structural/adapter/MovableAdapter.java @@ -0,0 +1,16 @@ +package src.main.java.com.designpatterns.structural.adapter; + +/** + * An Adapter pattern acts as a connector between two incompatible interfaces that otherwise cannot be connected + * directly. An Adapter wraps an existing class with a new interface so that it becomes compatible with the client’s + * interface. + *
+ * The main motive behind using this pattern is to convert an existing interface into another interface that the client + * expects. It’s usually implemented once the application is designed. + * + * @see Adapter Pattern + */ +public interface MovableAdapter { + // Returns the speed of the movable in KPH + double getSpeed(); +} diff --git a/src/main/java/com/designpatterns/structural/adapter/MovableAdapterImpl.java b/src/main/java/com/designpatterns/structural/adapter/MovableAdapterImpl.java new file mode 100644 index 00000000..c8d819a1 --- /dev/null +++ b/src/main/java/com/designpatterns/structural/adapter/MovableAdapterImpl.java @@ -0,0 +1,18 @@ +package src.main.java.com.designpatterns.structural.adapter; + +public class MovableAdapterImpl implements MovableAdapter { + private Movable luxuryCars; + + public MovableAdapterImpl(Movable luxuryCars) { + this.luxuryCars = luxuryCars; + } + + @Override + public double getSpeed() { + return convertMPHtoKMPH(luxuryCars.getSpeed()); + } + + private double convertMPHtoKMPH(double mph) { + return mph * 1.60934; + } +} diff --git a/src/test/java/com/designpatterns/structural/MovableAdapterTest.java b/src/test/java/com/designpatterns/structural/MovableAdapterTest.java new file mode 100644 index 00000000..bfec64bb --- /dev/null +++ b/src/test/java/com/designpatterns/structural/MovableAdapterTest.java @@ -0,0 +1,18 @@ +package src.test.java.com.designpatterns.structural; + +import org.junit.Test; +import src.main.java.com.designpatterns.structural.adapter.BugattiVeyron; +import src.main.java.com.designpatterns.structural.adapter.Movable; +import src.main.java.com.designpatterns.structural.adapter.MovableAdapter; +import src.main.java.com.designpatterns.structural.adapter.MovableAdapterImpl; + +import static org.junit.Assert.assertEquals; + +public class MovableAdapterTest { + @Test + public void testMovableAdapter() { + Movable bugattiVeyron = new BugattiVeyron(); + MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron); + assertEquals(bugattiVeyronAdapter.getSpeed(), 431.30312, 0.00001); + } +} From a7a9abd8c0f40082f5df5845e6ecce32d3ed2e81 Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Mon, 27 May 2019 12:06:39 +0530 Subject: [PATCH 2/3] Corrected the package for the JUnit --- .../structural/{ => adapter}/MovableAdapterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/test/java/com/designpatterns/structural/{ => adapter}/MovableAdapterTest.java (91%) diff --git a/src/test/java/com/designpatterns/structural/MovableAdapterTest.java b/src/test/java/com/designpatterns/structural/adapter/MovableAdapterTest.java similarity index 91% rename from src/test/java/com/designpatterns/structural/MovableAdapterTest.java rename to src/test/java/com/designpatterns/structural/adapter/MovableAdapterTest.java index bfec64bb..17ff9e49 100644 --- a/src/test/java/com/designpatterns/structural/MovableAdapterTest.java +++ b/src/test/java/com/designpatterns/structural/adapter/MovableAdapterTest.java @@ -1,4 +1,4 @@ -package src.test.java.com.designpatterns.structural; +package src.test.java.com.designpatterns.structural.adapter; import org.junit.Test; import src.main.java.com.designpatterns.structural.adapter.BugattiVeyron; From c31c39a08e658ebf5218864cf1f6ef7658039ef2 Mon Sep 17 00:00:00 2001 From: Abhijay Kumar Date: Tue, 28 May 2019 17:45:32 +0530 Subject: [PATCH 3/3] Removed usage of BigInteger constructor for constants --- src/main/java/com/conversions/BinaryToHexadecimal.java | 2 +- src/main/java/com/conversions/DecimalToHexadecimal.java | 2 +- src/main/java/com/conversions/DecimalToOctal.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/conversions/BinaryToHexadecimal.java b/src/main/java/com/conversions/BinaryToHexadecimal.java index fc891570..c7810f11 100644 --- a/src/main/java/com/conversions/BinaryToHexadecimal.java +++ b/src/main/java/com/conversions/BinaryToHexadecimal.java @@ -36,7 +36,7 @@ public class BinaryToHexadecimal { String hex = ""; int currentBit; - BigInteger tenValue = new BigInteger("10"); + BigInteger tenValue = BigInteger.valueOf(10); while (binary.compareTo(BigInteger.ZERO) != 0) { // to store decimal equivalent of number formed by 4 decimal digits int code4 = 0; diff --git a/src/main/java/com/conversions/DecimalToHexadecimal.java b/src/main/java/com/conversions/DecimalToHexadecimal.java index e0e9360d..0c812f52 100644 --- a/src/main/java/com/conversions/DecimalToHexadecimal.java +++ b/src/main/java/com/conversions/DecimalToHexadecimal.java @@ -4,7 +4,7 @@ import java.math.BigInteger; public class DecimalToHexadecimal { private static final char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; - private static final BigInteger valueHex = new BigInteger("16"); + private static final BigInteger valueHex = BigInteger.valueOf(16); /** * This method converts and decimal number to a Hexadecimal number diff --git a/src/main/java/com/conversions/DecimalToOctal.java b/src/main/java/com/conversions/DecimalToOctal.java index 6ab6e72b..64542298 100644 --- a/src/main/java/com/conversions/DecimalToOctal.java +++ b/src/main/java/com/conversions/DecimalToOctal.java @@ -4,7 +4,7 @@ import java.math.BigInteger; public class DecimalToOctal { private static final char[] octalChars = {'0', '1', '2', '3', '4', '5', '6', '7'}; - private static final BigInteger valueOctal = new BigInteger("8"); + private static final BigInteger valueOctal = BigInteger.valueOf(8); /** * This method converts and decimal number to a octal number