From 3a9654370af0659ed23bbafc8ea7d81c112b4da7 Mon Sep 17 00:00:00 2001 From: ramansahasi Date: Mon, 18 Sep 2017 05:29:23 +0530 Subject: [PATCH] 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 --- .../adapter/AdapterPatternDriver.java | 18 +++++----- .../designpatterns/adapter/AstonMartin.java | 4 +-- .../designpatterns/adapter/BugattiVeyron.java | 4 +-- .../designpatterns/adapter/LuxuryCars.java | 5 --- .../adapter/LuxuryCarsAdapter.java | 5 --- .../designpatterns/adapter/McLaren.java | 4 +-- .../designpatterns/adapter/Movable.java | 6 ++++ .../adapter/MovableAdapter.java | 6 ++++ ...apterImpl.java => MovableAdapterImpl.java} | 10 +++--- .../baeldung/designpatterns/bridge/Blue.java | 8 ++--- .../bridge/BridgePatternDriver.java | 4 +-- .../baeldung/designpatterns/bridge/Color.java | 2 +- .../baeldung/designpatterns/bridge/Red.java | 6 ++-- .../baeldung/designpatterns/bridge/Shape.java | 2 +- .../designpatterns/bridge/Square.java | 7 ++-- .../designpatterns/bridge/Triangle.java | 7 ++-- .../decorator/ChristmasTree.java | 4 +-- .../designpatterns/proxy/ExpensiveObject.java | 2 +- .../AdapterPatternIntegrationTest.java | 20 ++++++++--- .../BridgePatternIntegrationTest.java | 34 +++---------------- .../DecoratorPatternIntegrationTest.java | 19 +++++------ 21 files changed, 74 insertions(+), 103 deletions(-) delete mode 100644 core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCars.java delete mode 100644 core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapter.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/adapter/Movable.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/adapter/MovableAdapter.java rename core-java/src/main/java/com/baeldung/designpatterns/adapter/{LuxuryCarsAdapterImpl.java => MovableAdapterImpl.java} (50%) diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/AdapterPatternDriver.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/AdapterPatternDriver.java index 9d4fcf3574..79f618d038 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/adapter/AdapterPatternDriver.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/AdapterPatternDriver.java @@ -5,16 +5,16 @@ import static com.baeldung.designpatterns.util.LogerUtil.LOG; public class AdapterPatternDriver { public static void main(String args[]) { - LuxuryCars bugattiVeyron = new BugattiVeyron(); - LuxuryCarsAdapter bugattiVeyronAdapter = new LuxuryCarsAdapterImpl(bugattiVeyron); - LOG.info("Bugatti Veyron Super Sport's top speed is " + bugattiVeyronAdapter.speedInKMPH() + " Kmph."); + Movable bugattiVeyron = new BugattiVeyron(); + MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron); + LOG.info("Bugatti Veyron Super Sport's top speed is " + bugattiVeyronAdapter.getSpeed() + " Kmph."); - LuxuryCars mcLaren = new McLaren(); - LuxuryCarsAdapter mcLarenAdapter = new LuxuryCarsAdapterImpl(mcLaren); - LOG.info("McLaren F1 top speed is " + mcLarenAdapter.speedInKMPH() + " Kmph."); + Movable mcLaren = new McLaren(); + MovableAdapter mcLarenAdapter = new MovableAdapterImpl(mcLaren); + LOG.info("McLaren F1 top speed is " + mcLarenAdapter.getSpeed() + " Kmph."); - LuxuryCars astonMartin = new AstonMartin(); - LuxuryCarsAdapter astonMartinAdapter = new LuxuryCarsAdapterImpl(astonMartin); - LOG.info("McLaren F1 top speed is " + astonMartinAdapter.speedInKMPH() + " Kmph."); + Movable astonMartin = new AstonMartin(); + MovableAdapter astonMartinAdapter = new MovableAdapterImpl(astonMartin); + LOG.info("McLaren F1 top speed is " + astonMartinAdapter.getSpeed() + " Kmph."); } } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/AstonMartin.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/AstonMartin.java index b1fba0dae3..7dd83079a2 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/adapter/AstonMartin.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/AstonMartin.java @@ -1,8 +1,8 @@ package com.baeldung.designpatterns.adapter; -public class AstonMartin implements LuxuryCars { +public class AstonMartin implements Movable { @Override - public double speedInMPH() { + public double getSpeed() { return 220; } } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/BugattiVeyron.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/BugattiVeyron.java index 1b0b834448..a249d64b6f 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/adapter/BugattiVeyron.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/BugattiVeyron.java @@ -1,8 +1,8 @@ package com.baeldung.designpatterns.adapter; -public class BugattiVeyron implements LuxuryCars { +public class BugattiVeyron implements Movable { @Override - public double speedInMPH() { + public double getSpeed() { return 268; } } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCars.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCars.java deleted file mode 100644 index 9926f5f8bc..0000000000 --- a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCars.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.baeldung.designpatterns.adapter; - -public interface LuxuryCars { - public double speedInMPH(); -} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapter.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapter.java deleted file mode 100644 index f945e1b389..0000000000 --- a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapter.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.baeldung.designpatterns.adapter; - -public interface LuxuryCarsAdapter { - public double speedInKMPH(); -} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/McLaren.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/McLaren.java index e2ba2b830d..c807df67db 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/adapter/McLaren.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/McLaren.java @@ -1,8 +1,8 @@ package com.baeldung.designpatterns.adapter; -public class McLaren implements LuxuryCars { +public class McLaren implements Movable { @Override - public double speedInMPH() { + public double getSpeed() { return 241; } } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/Movable.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/Movable.java new file mode 100644 index 0000000000..ec94e90af0 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/Movable.java @@ -0,0 +1,6 @@ +package com.baeldung.designpatterns.adapter; + +public interface Movable { + // returns speed in MPH + double getSpeed(); +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/MovableAdapter.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/MovableAdapter.java new file mode 100644 index 0000000000..b9c7484446 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/MovableAdapter.java @@ -0,0 +1,6 @@ +package com.baeldung.designpatterns.adapter; + +public interface MovableAdapter { + // returns speed in KMPH + double getSpeed(); +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapterImpl.java b/core-java/src/main/java/com/baeldung/designpatterns/adapter/MovableAdapterImpl.java similarity index 50% rename from core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapterImpl.java rename to core-java/src/main/java/com/baeldung/designpatterns/adapter/MovableAdapterImpl.java index f2bf553292..eb74641389 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/adapter/LuxuryCarsAdapterImpl.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/adapter/MovableAdapterImpl.java @@ -1,15 +1,15 @@ package com.baeldung.designpatterns.adapter; -public class LuxuryCarsAdapterImpl implements LuxuryCarsAdapter { - private LuxuryCars luxuryCars; +public class MovableAdapterImpl implements MovableAdapter { + private Movable luxuryCars; - public LuxuryCarsAdapterImpl(LuxuryCars luxuryCars) { + public MovableAdapterImpl(Movable luxuryCars) { this.luxuryCars = luxuryCars; } @Override - public double speedInKMPH() { - double mph = luxuryCars.speedInMPH(); + public double getSpeed() { + double mph = luxuryCars.getSpeed(); return convertMPHtoKMPH(mph); } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Blue.java b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Blue.java index ed3f75b4a1..da5d29617f 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Blue.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Blue.java @@ -1,12 +1,8 @@ package com.baeldung.designpatterns.bridge; -import static com.baeldung.designpatterns.util.LogerUtil.LOG; - public class Blue implements Color { - @Override - public void fillColor() { - LOG.info("Color : Blue"); + public String fill() { + return "Color is Blue"; } - } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/bridge/BridgePatternDriver.java b/core-java/src/main/java/com/baeldung/designpatterns/bridge/BridgePatternDriver.java index 921deadcac..e6a7fb41c1 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/bridge/BridgePatternDriver.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/bridge/BridgePatternDriver.java @@ -5,10 +5,10 @@ public class BridgePatternDriver { public static void main(String[] args) { //a square with red color Shape square = new Square(new Red()); - square.drawShape(); + System.out.println(square.draw()); //a triangle with blue color Shape triangle = new Triangle(new Blue()); - triangle.drawShape(); + System.out.println(triangle.draw()); } } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Color.java b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Color.java index 91d2b01609..05618e6d6e 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Color.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Color.java @@ -1,5 +1,5 @@ package com.baeldung.designpatterns.bridge; public interface Color { - public void fillColor(); + String fill(); } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Red.java b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Red.java index 8c22a94f00..bc83199591 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Red.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Red.java @@ -1,12 +1,10 @@ package com.baeldung.designpatterns.bridge; -import static com.baeldung.designpatterns.util.LogerUtil.LOG; - public class Red implements Color { @Override - public void fillColor() { - LOG.info("Color : Red"); + public String fill() { + return "Color is Red"; } } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Shape.java b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Shape.java index c4daf7a821..75cd43dbc8 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Shape.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Shape.java @@ -7,5 +7,5 @@ public abstract class Shape { this.color = color; } - abstract public void drawShape(); + abstract public String draw(); } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Square.java b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Square.java index 6b377197eb..7397f4bd47 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Square.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Square.java @@ -1,7 +1,5 @@ package com.baeldung.designpatterns.bridge; -import static com.baeldung.designpatterns.util.LogerUtil.LOG; - public class Square extends Shape { public Square(Color color) { @@ -9,8 +7,7 @@ public class Square extends Shape { } @Override - public void drawShape() { - LOG.info("Square drawn. "); - color.fillColor(); + public String draw() { + return "Square drawn. " + color.fill(); } } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Triangle.java b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Triangle.java index 900e78cf2b..46db66ee42 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/bridge/Triangle.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/bridge/Triangle.java @@ -1,7 +1,5 @@ package com.baeldung.designpatterns.bridge; -import static com.baeldung.designpatterns.util.LogerUtil.LOG; - public class Triangle extends Shape { public Triangle(Color color) { @@ -9,8 +7,7 @@ public class Triangle extends Shape { } @Override - public void drawShape() { - LOG.info("Triangle drawn. "); - color.fillColor(); + public String draw() { + return "Triangle drawn. "+ color.fill(); } } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/decorator/ChristmasTree.java b/core-java/src/main/java/com/baeldung/designpatterns/decorator/ChristmasTree.java index 80a0865567..e5dca41dd8 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/decorator/ChristmasTree.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/decorator/ChristmasTree.java @@ -1,5 +1,5 @@ package com.baeldung.designpatterns.decorator; public interface ChristmasTree { - public String decorate(); -} + String decorate(); +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/designpatterns/proxy/ExpensiveObject.java b/core-java/src/main/java/com/baeldung/designpatterns/proxy/ExpensiveObject.java index 96a6bfb878..256b31bc84 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/proxy/ExpensiveObject.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/proxy/ExpensiveObject.java @@ -1,5 +1,5 @@ package com.baeldung.designpatterns.proxy; public interface ExpensiveObject { - public void process(); + void process(); } diff --git a/core-java/src/test/java/com/baeldung/designpatterns/AdapterPatternIntegrationTest.java b/core-java/src/test/java/com/baeldung/designpatterns/AdapterPatternIntegrationTest.java index e56e271743..8cad1290d9 100644 --- a/core-java/src/test/java/com/baeldung/designpatterns/AdapterPatternIntegrationTest.java +++ b/core-java/src/test/java/com/baeldung/designpatterns/AdapterPatternIntegrationTest.java @@ -6,15 +6,25 @@ import org.junit.Test; import com.baeldung.designpatterns.adapter.AstonMartin; import com.baeldung.designpatterns.adapter.BugattiVeyron; -import com.baeldung.designpatterns.adapter.LuxuryCarsAdapterImpl; 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 { @Test - public void givenLuxuryCarsAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() { - 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); + public void givenMovableAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() { + Movable bugattiVeyron = new BugattiVeyron(); + MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron); + 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); } } diff --git a/core-java/src/test/java/com/baeldung/designpatterns/BridgePatternIntegrationTest.java b/core-java/src/test/java/com/baeldung/designpatterns/BridgePatternIntegrationTest.java index 56a7a704f2..ed7eb0c453 100644 --- a/core-java/src/test/java/com/baeldung/designpatterns/BridgePatternIntegrationTest.java +++ b/core-java/src/test/java/com/baeldung/designpatterns/BridgePatternIntegrationTest.java @@ -1,14 +1,7 @@ package com.baeldung.designpatterns; -import static com.baeldung.designpatterns.util.LogerUtil.LOG; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.*; -import java.util.List; - -import org.apache.log4j.spi.LoggingEvent; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import com.baeldung.designpatterns.bridge.Blue; @@ -18,35 +11,16 @@ import com.baeldung.designpatterns.bridge.Square; import com.baeldung.designpatterns.bridge.Triangle; public class BridgePatternIntegrationTest { - public static TestAppenderDP appender; - - @Before - public void setUp() { - appender = new TestAppenderDP(); - LOG.addAppender(appender); - } - + @Test public void whenBridgePatternInvoked_thenConfigSuccess() { //a square with red color Shape square = new Square(new Red()); - square.drawShape(); + assertEquals(square.draw(), "Square drawn. Color is Red"); //a triangle with blue color Shape triangle = new Triangle(new Blue()); - triangle.drawShape(); - - final List 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); + assertEquals(triangle.draw(), "Triangle drawn. Color is Blue"); } } diff --git a/core-java/src/test/java/com/baeldung/designpatterns/DecoratorPatternIntegrationTest.java b/core-java/src/test/java/com/baeldung/designpatterns/DecoratorPatternIntegrationTest.java index b3b3f988f1..de0ca8a135 100644 --- a/core-java/src/test/java/com/baeldung/designpatterns/DecoratorPatternIntegrationTest.java +++ b/core-java/src/test/java/com/baeldung/designpatterns/DecoratorPatternIntegrationTest.java @@ -10,19 +10,16 @@ import com.baeldung.designpatterns.decorator.ChristmasTreeImpl; import com.baeldung.designpatterns.decorator.Garland; public class DecoratorPatternIntegrationTest { - private ChristmasTree tree; - @Test public void givenDecoratorPattern_WhenDecoratorsInjectedAtRuntime_thenConfigSuccess() { - //christmas tree with just one Garland - tree = new Garland(new ChristmasTreeImpl()); - assertEquals(tree.decorate(), "Christmas tree with Garland"); - - //christmas tree with two Garlands and one Bubble lights - tree = new BubbleLights(new Garland( - new Garland(new ChristmasTreeImpl())) - ); - assertEquals(tree.decorate(), "Christmas tree with Garland with Garland with Bubble Lights"); + ChristmasTree tree1 = new Garland(new ChristmasTreeImpl()); + assertEquals(tree1.decorate(), + "Christmas tree with Garland"); + + ChristmasTree tree2 = new BubbleLights( + new Garland(new Garland(new ChristmasTreeImpl()))); + assertEquals(tree2.decorate(), + "Christmas tree with Garland with Garland with Bubble Lights"); } }