Bridge design pattern which is also a structural design pattern is a pattern where you can decouple the abstraction and implementation using a bridge so that both can vary without effecting each other.
interface DrawingAPI {
public void drawCircle(final double x, final double y, final double radius);
}
/** "ConcreteImplementor" 1/2 */
class DrawingAPI1 implements DrawingAPI {
public void drawCircle(final double x, final double y, final double radius) {
System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);
}
}
/** "ConcreteImplementor" 2/2 */
class DrawingAPI2 implements DrawingAPI {
public void drawCircle(final double x, final double y, final double radius) {
System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);
}
}
/** "Abstraction" */
abstract class Shape {
protected DrawingAPI drawingAPI;
protected Shape(final DrawingAPI drawingAPI){
this.drawingAPI = drawingAPI;
}
public abstract void draw(); // low-level
public abstract void resizeByPercentage(final double pct); // high-level
}
/** "Refined Abstraction" */
class CircleShape extends Shape {
private double x, y, radius;
public CircleShape(final double x, final double y, final double radius, final DrawingAPI drawingAPI) {
super(drawingAPI);
this.x = x; this.y = y; this.radius = radius;
}
// low-level i.e. Implementation specific
public void draw() {
drawingAPI.drawCircle(x, y, radius);
}
// high-level i.e. Abstraction specific
public void resizeByPercentage(final double pct) {
radius *= (1.0 + pct/100.0);
}
}
/** "Client" */
class BridgePattern {
public static void main(final String[] args) {
Shape[] shapes = new Shape[] {
new CircleShape(1, 2, 3, new DrawingAPI1()),
new CircleShape(5, 7, 11, new DrawingAPI2())
};
for (Shape shape : shapes) {
shape.resizeByPercentage(2.5);
shape.draw();
}
}
}
Let’s
take the above class diagram as our use case where a circle can be drawn in
different colors using the same abstract class method but different bridge implementer’s
class.So
here we have abstract class shape and it has method draw which can vary, so we
have abstracted it out to an interface using aggregation to bridge interface "DrawAPI" and it is responsibility of its implementer’s to draw different color
circle.
** "Bridge Implementor" */interface DrawingAPI {
public void drawCircle(final double x, final double y, final double radius);
}
/** "ConcreteImplementor" 1/2 */
class DrawingAPI1 implements DrawingAPI {
public void drawCircle(final double x, final double y, final double radius) {
System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);
}
}
/** "ConcreteImplementor" 2/2 */
class DrawingAPI2 implements DrawingAPI {
public void drawCircle(final double x, final double y, final double radius) {
System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);
}
}
/** "Abstraction" */
abstract class Shape {
protected DrawingAPI drawingAPI;
protected Shape(final DrawingAPI drawingAPI){
this.drawingAPI = drawingAPI;
}
public abstract void draw(); // low-level
public abstract void resizeByPercentage(final double pct); // high-level
}
/** "Refined Abstraction" */
class CircleShape extends Shape {
private double x, y, radius;
public CircleShape(final double x, final double y, final double radius, final DrawingAPI drawingAPI) {
super(drawingAPI);
this.x = x; this.y = y; this.radius = radius;
}
// low-level i.e. Implementation specific
public void draw() {
drawingAPI.drawCircle(x, y, radius);
}
// high-level i.e. Abstraction specific
public void resizeByPercentage(final double pct) {
radius *= (1.0 + pct/100.0);
}
}
/** "Client" */
class BridgePattern {
public static void main(final String[] args) {
Shape[] shapes = new Shape[] {
new CircleShape(1, 2, 3, new DrawingAPI1()),
new CircleShape(5, 7, 11, new DrawingAPI2())
};
for (Shape shape : shapes) {
shape.resizeByPercentage(2.5);
shape.draw();
}
}
}
Comments
Post a Comment