Merge pull request #769 from abhijay94/Development
Added adapter pattern and its test case
This commit is contained in:
commit
837f635001
@ -36,7 +36,7 @@ public class BinaryToHexadecimal {
|
|||||||
String hex = "";
|
String hex = "";
|
||||||
|
|
||||||
int currentBit;
|
int currentBit;
|
||||||
BigInteger tenValue = new BigInteger("10");
|
BigInteger tenValue = BigInteger.valueOf(10);
|
||||||
while (binary.compareTo(BigInteger.ZERO) != 0) {
|
while (binary.compareTo(BigInteger.ZERO) != 0) {
|
||||||
// to store decimal equivalent of number formed by 4 decimal digits
|
// to store decimal equivalent of number formed by 4 decimal digits
|
||||||
int code4 = 0;
|
int code4 = 0;
|
||||||
|
@ -4,7 +4,7 @@ import java.math.BigInteger;
|
|||||||
|
|
||||||
public class DecimalToHexadecimal {
|
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 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
|
* This method converts and decimal number to a Hexadecimal number
|
||||||
|
@ -4,7 +4,7 @@ import java.math.BigInteger;
|
|||||||
|
|
||||||
public class DecimalToOctal {
|
public class DecimalToOctal {
|
||||||
private static final char[] octalChars = {'0', '1', '2', '3', '4', '5', '6', '7'};
|
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
|
* This method converts and decimal number to a octal number
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
package src.main.java.com.designpatterns.structural.adapter;
|
||||||
|
|
||||||
|
public class BugattiVeyron implements Movable {
|
||||||
|
@Override
|
||||||
|
public double getSpeed() {
|
||||||
|
return 268;
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user