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:
ramansahasi 2017-08-30 02:55:11 +05:30 committed by maibin
parent 3c5ed71063
commit 5945d5ceb3
11 changed files with 72 additions and 62 deletions

View File

@ -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.");
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.designpatterns.adapter;
public class AstonMartin implements LuxuryCars {
@Override
public double speedInMPH() {
return 220;
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.designpatterns.adapter;
public class BugattiVeyron implements LuxuryCars {
@Override
public double speedInMPH() {
return 268;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.adapter;
public interface LuxuryCars {
public double speedInMPH();
}

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.adapter;
public interface LuxuryCarsAdapter {
public double speedInKMPH();
}

View File

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

View File

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

View File

@ -1,9 +0,0 @@
package com.baeldung.designpatterns.adapter;
public interface LuxuryCarsSpeedAdapter {
public double bugattiVeyronInKMPH();
public double mcLarenInKMPH();
public double astonMartinInKMPH();
}

View File

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

View File

@ -0,0 +1,8 @@
package com.baeldung.designpatterns.adapter;
public class McLaren implements LuxuryCars {
@Override
public double speedInMPH() {
return 241;
}
}

View File

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