BAEL-1072 Difference between Proxy, Decorator, Adapter, and Bridge Patterns (#2635)
* Changes as per last review. Changed method names so that they look more idiomatic. * Updated pattern, changed method names and test cases
This commit is contained in:
parent
a323d64d75
commit
3a9654370a
|
@ -5,16 +5,16 @@ import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
||||||
public class AdapterPatternDriver {
|
public class AdapterPatternDriver {
|
||||||
|
|
||||||
public static void main(String args[]) {
|
public static void main(String args[]) {
|
||||||
LuxuryCars bugattiVeyron = new BugattiVeyron();
|
Movable bugattiVeyron = new BugattiVeyron();
|
||||||
LuxuryCarsAdapter bugattiVeyronAdapter = new LuxuryCarsAdapterImpl(bugattiVeyron);
|
MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron);
|
||||||
LOG.info("Bugatti Veyron Super Sport's top speed is " + bugattiVeyronAdapter.speedInKMPH() + " Kmph.");
|
LOG.info("Bugatti Veyron Super Sport's top speed is " + bugattiVeyronAdapter.getSpeed() + " Kmph.");
|
||||||
|
|
||||||
LuxuryCars mcLaren = new McLaren();
|
Movable mcLaren = new McLaren();
|
||||||
LuxuryCarsAdapter mcLarenAdapter = new LuxuryCarsAdapterImpl(mcLaren);
|
MovableAdapter mcLarenAdapter = new MovableAdapterImpl(mcLaren);
|
||||||
LOG.info("McLaren F1 top speed is " + mcLarenAdapter.speedInKMPH() + " Kmph.");
|
LOG.info("McLaren F1 top speed is " + mcLarenAdapter.getSpeed() + " Kmph.");
|
||||||
|
|
||||||
LuxuryCars astonMartin = new AstonMartin();
|
Movable astonMartin = new AstonMartin();
|
||||||
LuxuryCarsAdapter astonMartinAdapter = new LuxuryCarsAdapterImpl(astonMartin);
|
MovableAdapter astonMartinAdapter = new MovableAdapterImpl(astonMartin);
|
||||||
LOG.info("McLaren F1 top speed is " + astonMartinAdapter.speedInKMPH() + " Kmph.");
|
LOG.info("McLaren F1 top speed is " + astonMartinAdapter.getSpeed() + " Kmph.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package com.baeldung.designpatterns.adapter;
|
package com.baeldung.designpatterns.adapter;
|
||||||
|
|
||||||
public class AstonMartin implements LuxuryCars {
|
public class AstonMartin implements Movable {
|
||||||
@Override
|
@Override
|
||||||
public double speedInMPH() {
|
public double getSpeed() {
|
||||||
return 220;
|
return 220;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package com.baeldung.designpatterns.adapter;
|
package com.baeldung.designpatterns.adapter;
|
||||||
|
|
||||||
public class BugattiVeyron implements LuxuryCars {
|
public class BugattiVeyron implements Movable {
|
||||||
@Override
|
@Override
|
||||||
public double speedInMPH() {
|
public double getSpeed() {
|
||||||
return 268;
|
return 268;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package com.baeldung.designpatterns.adapter;
|
|
||||||
|
|
||||||
public interface LuxuryCars {
|
|
||||||
public double speedInMPH();
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
package com.baeldung.designpatterns.adapter;
|
|
||||||
|
|
||||||
public interface LuxuryCarsAdapter {
|
|
||||||
public double speedInKMPH();
|
|
||||||
}
|
|
|
@ -1,8 +1,8 @@
|
||||||
package com.baeldung.designpatterns.adapter;
|
package com.baeldung.designpatterns.adapter;
|
||||||
|
|
||||||
public class McLaren implements LuxuryCars {
|
public class McLaren implements Movable {
|
||||||
@Override
|
@Override
|
||||||
public double speedInMPH() {
|
public double getSpeed() {
|
||||||
return 241;
|
return 241;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
package com.baeldung.designpatterns.adapter;
|
||||||
|
|
||||||
|
public interface Movable {
|
||||||
|
// returns speed in MPH
|
||||||
|
double getSpeed();
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package com.baeldung.designpatterns.adapter;
|
||||||
|
|
||||||
|
public interface MovableAdapter {
|
||||||
|
// returns speed in KMPH
|
||||||
|
double getSpeed();
|
||||||
|
}
|
|
@ -1,15 +1,15 @@
|
||||||
package com.baeldung.designpatterns.adapter;
|
package com.baeldung.designpatterns.adapter;
|
||||||
|
|
||||||
public class LuxuryCarsAdapterImpl implements LuxuryCarsAdapter {
|
public class MovableAdapterImpl implements MovableAdapter {
|
||||||
private LuxuryCars luxuryCars;
|
private Movable luxuryCars;
|
||||||
|
|
||||||
public LuxuryCarsAdapterImpl(LuxuryCars luxuryCars) {
|
public MovableAdapterImpl(Movable luxuryCars) {
|
||||||
this.luxuryCars = luxuryCars;
|
this.luxuryCars = luxuryCars;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double speedInKMPH() {
|
public double getSpeed() {
|
||||||
double mph = luxuryCars.speedInMPH();
|
double mph = luxuryCars.getSpeed();
|
||||||
return convertMPHtoKMPH(mph);
|
return convertMPHtoKMPH(mph);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
package com.baeldung.designpatterns.bridge;
|
package com.baeldung.designpatterns.bridge;
|
||||||
|
|
||||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
|
||||||
|
|
||||||
public class Blue implements Color {
|
public class Blue implements Color {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fillColor() {
|
public String fill() {
|
||||||
LOG.info("Color : Blue");
|
return "Color is Blue";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,10 @@ public class BridgePatternDriver {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//a square with red color
|
//a square with red color
|
||||||
Shape square = new Square(new Red());
|
Shape square = new Square(new Red());
|
||||||
square.drawShape();
|
System.out.println(square.draw());
|
||||||
|
|
||||||
//a triangle with blue color
|
//a triangle with blue color
|
||||||
Shape triangle = new Triangle(new Blue());
|
Shape triangle = new Triangle(new Blue());
|
||||||
triangle.drawShape();
|
System.out.println(triangle.draw());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
package com.baeldung.designpatterns.bridge;
|
package com.baeldung.designpatterns.bridge;
|
||||||
|
|
||||||
public interface Color {
|
public interface Color {
|
||||||
public void fillColor();
|
String fill();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
package com.baeldung.designpatterns.bridge;
|
package com.baeldung.designpatterns.bridge;
|
||||||
|
|
||||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
|
||||||
|
|
||||||
public class Red implements Color {
|
public class Red implements Color {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fillColor() {
|
public String fill() {
|
||||||
LOG.info("Color : Red");
|
return "Color is Red";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,5 +7,5 @@ public abstract class Shape {
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract public void drawShape();
|
abstract public String draw();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package com.baeldung.designpatterns.bridge;
|
package com.baeldung.designpatterns.bridge;
|
||||||
|
|
||||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
|
||||||
|
|
||||||
public class Square extends Shape {
|
public class Square extends Shape {
|
||||||
|
|
||||||
public Square(Color color) {
|
public Square(Color color) {
|
||||||
|
@ -9,8 +7,7 @@ public class Square extends Shape {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawShape() {
|
public String draw() {
|
||||||
LOG.info("Square drawn. ");
|
return "Square drawn. " + color.fill();
|
||||||
color.fillColor();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package com.baeldung.designpatterns.bridge;
|
package com.baeldung.designpatterns.bridge;
|
||||||
|
|
||||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
|
||||||
|
|
||||||
public class Triangle extends Shape {
|
public class Triangle extends Shape {
|
||||||
|
|
||||||
public Triangle(Color color) {
|
public Triangle(Color color) {
|
||||||
|
@ -9,8 +7,7 @@ public class Triangle extends Shape {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawShape() {
|
public String draw() {
|
||||||
LOG.info("Triangle drawn. ");
|
return "Triangle drawn. "+ color.fill();
|
||||||
color.fillColor();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
package com.baeldung.designpatterns.decorator;
|
package com.baeldung.designpatterns.decorator;
|
||||||
|
|
||||||
public interface ChristmasTree {
|
public interface ChristmasTree {
|
||||||
public String decorate();
|
String decorate();
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
package com.baeldung.designpatterns.proxy;
|
package com.baeldung.designpatterns.proxy;
|
||||||
|
|
||||||
public interface ExpensiveObject {
|
public interface ExpensiveObject {
|
||||||
public void process();
|
void process();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,15 +6,25 @@ import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.designpatterns.adapter.AstonMartin;
|
import com.baeldung.designpatterns.adapter.AstonMartin;
|
||||||
import com.baeldung.designpatterns.adapter.BugattiVeyron;
|
import com.baeldung.designpatterns.adapter.BugattiVeyron;
|
||||||
import com.baeldung.designpatterns.adapter.LuxuryCarsAdapterImpl;
|
|
||||||
import com.baeldung.designpatterns.adapter.McLaren;
|
import com.baeldung.designpatterns.adapter.McLaren;
|
||||||
|
import com.baeldung.designpatterns.adapter.Movable;
|
||||||
|
import com.baeldung.designpatterns.adapter.MovableAdapter;
|
||||||
|
import com.baeldung.designpatterns.adapter.MovableAdapterImpl;
|
||||||
|
|
||||||
public class AdapterPatternIntegrationTest {
|
public class AdapterPatternIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenLuxuryCarsAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() {
|
public void givenMovableAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() {
|
||||||
assertEquals(new LuxuryCarsAdapterImpl(new BugattiVeyron()).speedInKMPH(), 431.30312, 0.00001);
|
Movable bugattiVeyron = new BugattiVeyron();
|
||||||
assertEquals(new LuxuryCarsAdapterImpl(new McLaren()).speedInKMPH(), 387.85094, 0.00001);
|
MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron);
|
||||||
assertEquals(new LuxuryCarsAdapterImpl(new AstonMartin()).speedInKMPH(), 354.0548, 0.00001);
|
assertEquals(bugattiVeyronAdapter.getSpeed(), 431.30312, 0.00001);
|
||||||
|
|
||||||
|
Movable mcLaren = new McLaren();
|
||||||
|
MovableAdapter mcLarenAdapter = new MovableAdapterImpl(mcLaren);
|
||||||
|
assertEquals(mcLarenAdapter.getSpeed(), 387.85094, 0.00001);
|
||||||
|
|
||||||
|
Movable astonMartin = new AstonMartin();
|
||||||
|
MovableAdapter astonMartinAdapter = new MovableAdapterImpl(astonMartin);
|
||||||
|
assertEquals(astonMartinAdapter.getSpeed(), 354.0548, 0.00001);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,7 @@
|
||||||
package com.baeldung.designpatterns;
|
package com.baeldung.designpatterns;
|
||||||
|
|
||||||
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
|
import static org.junit.Assert.*;
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.log4j.spi.LoggingEvent;
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.designpatterns.bridge.Blue;
|
import com.baeldung.designpatterns.bridge.Blue;
|
||||||
|
@ -18,35 +11,16 @@ import com.baeldung.designpatterns.bridge.Square;
|
||||||
import com.baeldung.designpatterns.bridge.Triangle;
|
import com.baeldung.designpatterns.bridge.Triangle;
|
||||||
|
|
||||||
public class BridgePatternIntegrationTest {
|
public class BridgePatternIntegrationTest {
|
||||||
public static TestAppenderDP appender;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
appender = new TestAppenderDP();
|
|
||||||
LOG.addAppender(appender);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenBridgePatternInvoked_thenConfigSuccess() {
|
public void whenBridgePatternInvoked_thenConfigSuccess() {
|
||||||
//a square with red color
|
//a square with red color
|
||||||
Shape square = new Square(new Red());
|
Shape square = new Square(new Red());
|
||||||
square.drawShape();
|
assertEquals(square.draw(), "Square drawn. Color is Red");
|
||||||
|
|
||||||
//a triangle with blue color
|
//a triangle with blue color
|
||||||
Shape triangle = new Triangle(new Blue());
|
Shape triangle = new Triangle(new Blue());
|
||||||
triangle.drawShape();
|
assertEquals(triangle.draw(), "Triangle drawn. Color is Blue");
|
||||||
|
|
||||||
final List<LoggingEvent> log = appender.getLog();
|
|
||||||
|
|
||||||
assertThat((String) log.get(0).getMessage(), is("Square drawn. "));
|
|
||||||
assertThat((String) log.get(1).getMessage(), is("Color : Red"));
|
|
||||||
assertThat((String) log.get(2).getMessage(), is("Triangle drawn. "));
|
|
||||||
assertThat((String) log.get(3).getMessage(), is("Color : Blue"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
LOG.removeAppender(appender);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,19 +10,16 @@ import com.baeldung.designpatterns.decorator.ChristmasTreeImpl;
|
||||||
import com.baeldung.designpatterns.decorator.Garland;
|
import com.baeldung.designpatterns.decorator.Garland;
|
||||||
|
|
||||||
public class DecoratorPatternIntegrationTest {
|
public class DecoratorPatternIntegrationTest {
|
||||||
private ChristmasTree tree;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDecoratorPattern_WhenDecoratorsInjectedAtRuntime_thenConfigSuccess() {
|
public void givenDecoratorPattern_WhenDecoratorsInjectedAtRuntime_thenConfigSuccess() {
|
||||||
//christmas tree with just one Garland
|
ChristmasTree tree1 = new Garland(new ChristmasTreeImpl());
|
||||||
tree = new Garland(new ChristmasTreeImpl());
|
assertEquals(tree1.decorate(),
|
||||||
assertEquals(tree.decorate(), "Christmas tree with Garland");
|
"Christmas tree with Garland");
|
||||||
|
|
||||||
//christmas tree with two Garlands and one Bubble lights
|
ChristmasTree tree2 = new BubbleLights(
|
||||||
tree = new BubbleLights(new Garland(
|
new Garland(new Garland(new ChristmasTreeImpl())));
|
||||||
new Garland(new ChristmasTreeImpl()))
|
assertEquals(tree2.decorate(),
|
||||||
);
|
"Christmas tree with Garland with Garland with Bubble Lights");
|
||||||
assertEquals(tree.decorate(), "Christmas tree with Garland with Garland with Bubble Lights");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue