Skip to content

Commit 415b80d

Browse files
ramansahasimaibin
authored andcommitted
BAEL-1072 - Difference between Proxy, Decorator, Adapter, and Bridge Patterns (eugenp#2390)
* Structural Design Patterns Difference between several types of structural design patterns * Structural Design Pattern Difference between several types of structural design patterns * Structural Design Patterns Difference between several types of structural design patterns * Structural Design Patterns Difference between several types of structural design patterns
1 parent 53433a0 commit 415b80d

29 files changed

Lines changed: 498 additions & 1 deletion

core-java/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@
8080
</dependency>
8181

8282
<!-- logging -->
83-
83+
<dependency>
84+
<groupId>log4j</groupId>
85+
<artifactId>log4j</artifactId>
86+
<version>1.2.17</version>
87+
</dependency>
8488
<dependency>
8589
<groupId>org.slf4j</groupId>
8690
<artifactId>slf4j-api</artifactId>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.designpatterns.adapter;
2+
3+
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
4+
5+
public class AdapterPatternDriver {
6+
7+
public static void main(String args[]) {
8+
LuxuryCarsSpeedAdapter luxuryCars = new LuxuryCarsSpeedAdapterImpl();
9+
LOG.info("Bugatti Veyron Super Sport's top speed is " + luxuryCars.bugattiVeyronInKMPH() + " Kmph.");
10+
LOG.info("McLaren F1 top speed is " + luxuryCars.mcLarenInKMPH() + " Kmph.");
11+
LOG.info("Aston Martin One-77 top speed is " + luxuryCars.astonMartinInKMPH() + " Kmph.");
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.designpatterns.adapter;
2+
3+
public class LuxuryCarsSpeed {
4+
public double bugattiVeyronInMPH() {
5+
return 268;
6+
}
7+
8+
public double mcLarenInMPH() {
9+
return 241;
10+
}
11+
12+
public double astonMartinInMPH() {
13+
return 220;
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.designpatterns.adapter;
2+
3+
public interface LuxuryCarsSpeedAdapter {
4+
public double bugattiVeyronInKMPH();
5+
public double mcLarenInKMPH();
6+
public double astonMartinInKMPH();
7+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.designpatterns.adapter;
2+
3+
public class LuxuryCarsSpeedAdapterImpl extends LuxuryCarsSpeed implements LuxuryCarsSpeedAdapter {
4+
5+
@Override
6+
public double bugattiVeyronInKMPH() {
7+
double mph = super.bugattiVeyronInMPH();
8+
return convertMPHtoKMPH(mph);
9+
}
10+
11+
@Override
12+
public double mcLarenInKMPH() {
13+
double mph = super.mcLarenInMPH();
14+
return convertMPHtoKMPH(mph);
15+
}
16+
17+
@Override
18+
public double astonMartinInKMPH() {
19+
double mph = super.astonMartinInMPH();
20+
return convertMPHtoKMPH(mph);
21+
}
22+
23+
private double convertMPHtoKMPH(double mph) {
24+
return mph * 1.60934;
25+
}
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.designpatterns.bridge;
2+
3+
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
4+
5+
public class Blue implements Color {
6+
7+
@Override
8+
public void fillColor() {
9+
LOG.info("Color : Blue");
10+
}
11+
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.designpatterns.bridge;
2+
3+
public class BridgePatternDriver {
4+
5+
public static void main(String[] args) {
6+
//a square with red color
7+
Shape square = new Square(new Red());
8+
square.drawShape();
9+
10+
//a triangle with blue color
11+
Shape triangle = new Triangle(new Blue());
12+
triangle.drawShape();
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.designpatterns.bridge;
2+
3+
public interface Color {
4+
public void fillColor();
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.designpatterns.bridge;
2+
3+
import static com.baeldung.designpatterns.util.LogerUtil.LOG;
4+
5+
public class Red implements Color {
6+
7+
@Override
8+
public void fillColor() {
9+
LOG.info("Color : Red");
10+
}
11+
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.designpatterns.bridge;
2+
3+
public abstract class Shape {
4+
protected Color color;
5+
6+
public Shape(Color color) {
7+
this.color = color;
8+
}
9+
10+
abstract public void drawShape();
11+
}

0 commit comments

Comments
 (0)