BAEL-1072 - Difference between Proxy, Decorator, Adapter, and Bridge (Update) (#2518)
* Removing old code * Removing old code * Removing old code * Removing old code * Updated design for Adapter pattern * Updated test cases for new design
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class AstonMartin implements LuxuryCars {
|
||||
@Override
|
||||
public double speedInMPH() {
|
||||
return 220;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class BugattiVeyron implements LuxuryCars {
|
||||
@Override
|
||||
public double speedInMPH() {
|
||||
return 268;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public interface LuxuryCars {
|
||||
public double speedInMPH();
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public interface LuxuryCarsAdapter {
|
||||
public double speedInKMPH();
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public interface LuxuryCarsSpeedAdapter {
|
||||
public double bugattiVeyronInKMPH();
|
||||
|
||||
public double mcLarenInKMPH();
|
||||
|
||||
public double astonMartinInKMPH();
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.designpatterns.adapter;
|
||||
|
||||
public class McLaren implements LuxuryCars {
|
||||
@Override
|
||||
public double speedInMPH() {
|
||||
return 241;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user