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:
ramansahasi 2017-09-18 05:29:23 +05:30 committed by maibin
parent a323d64d75
commit 3a9654370a
21 changed files with 74 additions and 103 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,6 @@
package com.baeldung.designpatterns.adapter;
public interface Movable {
// returns speed in MPH
double getSpeed();
}

View File

@ -0,0 +1,6 @@
package com.baeldung.designpatterns.adapter;
public interface MovableAdapter {
// returns speed in KMPH
double getSpeed();
}

View File

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

View File

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

View File

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

View File

@ -1,5 +1,5 @@
package com.baeldung.designpatterns.bridge;
public interface Color {
public void fillColor();
String fill();
}

View File

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

View File

@ -7,5 +7,5 @@ public abstract class Shape {
this.color = color;
}
abstract public void drawShape();
abstract public String draw();
}

View File

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

View File

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

View File

@ -1,5 +1,5 @@
package com.baeldung.designpatterns.decorator;
public interface ChristmasTree {
public String decorate();
String decorate();
}

View File

@ -1,5 +1,5 @@
package com.baeldung.designpatterns.proxy;
public interface ExpensiveObject {
public void process();
void process();
}

View File

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

View File

@ -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<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);
assertEquals(triangle.draw(), "Triangle drawn. Color is Blue");
}
}

View File

@ -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");
ChristmasTree tree1 = new Garland(new ChristmasTreeImpl());
assertEquals(tree1.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 tree2 = new BubbleLights(
new Garland(new Garland(new ChristmasTreeImpl())));
assertEquals(tree2.decorate(),
"Christmas tree with Garland with Garland with Bubble Lights");
}
}