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:
@@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
public class McLaren implements LuxuryCars {
|
||||
public class McLaren implements Movable {
|
||||
@Override
|
||||
public double speedInMPH() {
|
||||
public double getSpeed() {
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.baeldung.designpatterns.bridge;
|
||||
|
||||
public interface Color {
|
||||
public void fillColor();
|
||||
String fill();
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,5 +7,5 @@ public abstract class Shape {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
abstract public void drawShape();
|
||||
abstract public String draw();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.baeldung.designpatterns.decorator;
|
||||
|
||||
public interface ChristmasTree {
|
||||
public String decorate();
|
||||
}
|
||||
String decorate();
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.baeldung.designpatterns.proxy;
|
||||
|
||||
public interface ExpensiveObject {
|
||||
public void process();
|
||||
void process();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user