Skip to content

Added adapter pattern and its test case #769

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

Merged
merged 3 commits into from
May 29, 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
2 changes: 1 addition & 1 deletion src/main/java/com/conversions/BinaryToHexadecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public String binToHex(String binStr) {
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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/conversions/DecimalToHexadecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/conversions/DecimalToOctal.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package src.main.java.com.designpatterns.structural.adapter;

public class BugattiVeyron implements Movable {
@Override
public double getSpeed() {
return 268;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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.
* <br>
* 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 <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter Pattern</a>
*/
public interface MovableAdapter {
// Returns the speed of the movable in KPH
double getSpeed();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package src.test.java.com.designpatterns.structural.adapter;

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);
}
}