Skip to content

Commit 0d7650f

Browse files
authored
Merge pull request dubesar#586 from sourabhvarshney111/Bridge
Added Bridge Pattern
2 parents a851766 + 0101991 commit 0d7650f

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Reference: https://www.tutorialspoint.com/design_pattern/bridge_pattern.htm
3+
*/
4+
5+
interface DrawAPI {
6+
public void drawCircle(int radius, int x, int y);
7+
}
8+
9+
class RedCircle implements DrawAPI {
10+
@Override
11+
public void drawCircle(int radius, int x, int y) {
12+
System.out.println("Drawing Circle[ color: red, radius: " + radius + ", x: " + x + ", " + y + "]");
13+
}
14+
}
15+
16+
class GreenCircle implements DrawAPI {
17+
@Override
18+
public void drawCircle(int radius, int x, int y) {
19+
System.out.println("Drawing Circle[ color: green, radius: " + radius + ", x: " + x + ", " + y + "]");
20+
}
21+
}
22+
23+
abstract class Shape {
24+
protected DrawAPI drawAPI;
25+
26+
protected Shape(DrawAPI drawAPI){
27+
this.drawAPI = drawAPI;
28+
}
29+
public abstract void draw();
30+
}
31+
32+
class Circle extends Shape {
33+
private int x, y, radius;
34+
35+
public Circle(int x, int y, int radius, DrawAPI drawAPI) {
36+
super(drawAPI);
37+
this.x = x;
38+
this.y = y;
39+
this.radius = radius;
40+
}
41+
42+
public void draw() {
43+
drawAPI.drawCircle(radius,x,y);
44+
}
45+
}
46+
47+
public class BridgePattern {
48+
public static void main(String[] args) {
49+
Shape redCircle = new Circle(100,100, 10, new RedCircle());
50+
Shape greenCircle = new Circle(100,100, 10, new GreenCircle());
51+
52+
redCircle.draw();
53+
greenCircle.draw();
54+
}
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
### Bridge Pattern
2+
3+
### Introduction
4+
5+
Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern comes under structural pattern as this pattern decouples implementation class and abstract class by providing a bridge structure between them.
6+
7+
This pattern involves an interface which acts as a bridge which makes the functionality of concrete classes independent from interface implementer classes. Both types of classes can be altered structurally without affecting each other.
8+
9+
### Advantages
10+
11+
- It enables the separation of implementation from the interface.
12+
- It improves the extensibility.
13+
- It allows the hiding of implementation details from the client.
14+
15+
### Disadvantages
16+
17+
- Bridge pattern implementation increases code complexity.
18+
- Interfaces with only single implementation.
19+
- Multiple indirection - A level of indirection is introduced as the request is passed from the Abstraction to the actual implementor.
20+
21+
### Programming
22+
23+
We have a DrawAPI interface which is acting as a bridge implementer and concrete classes RedCircle, GreenCircle implementing the DrawAPI interface. Shape is an abstract class and will use object of DrawAPI. BridgePattern, our demo class will use Shape class to draw different colored circle.

0 commit comments

Comments
 (0)