Category : Design Patterns | Sub Category : Structural design patterns | By Prasad Bonam Last updated: 2023-07-09 14:23:18 Viewed : 77
The Bridge pattern is a structural design pattern that decouples an abstraction from its implementation, allowing them to vary independently. It enables the abstraction and implementation to be developed independently and changes in one do not affect the other. The Bridge pattern is useful when there are multiple dimensions of variation that need to be separated. Here is an example of implementing the Bridge pattern in Java:
java// Abstraction
abstract class Shape {
protected Color color;
public Shape(Color color) {
this.color = color;
}
public abstract void draw();
}
// Refined Abstraction
class Circle extends Shape {
private int x;
private int y;
private int radius;
public Circle(int x, int y, int radius, Color color) {
super(color);
this.x = x;
this.y = y;
this.radius = radius;
}
@Override
public void draw() {
System.out.print("Drawing Circle at (" + x + ", " + y + "), radius: " + radius + ", ");
color.applyColor();
}
}
// Refined Abstraction
class Rectangle extends Shape {
private int width;
private int height;
public Rectangle(int width, int height, Color color) {
super(color);
this.width = width;
this.height = height;
}
@Override
public void draw() {
System.out.print("Drawing Rectangle with width: " + width + ", height: " + height + ", ");
color.applyColor();
}
}
// Implementor
interface Color {
void applyColor();
}
// Concrete Implementor
class RedColor implements Color {
@Override
public void applyColor() {
System.out.println("Applying red color");
}
}
// Concrete Implementor
class GreenColor implements Color {
@Override
public void applyColor() {
System.out.println("Applying green color");
}
}
// Client code
public class Client {
public static void main(String[] args) {
Shape redCircle = new Circle(100, 100, 50, new RedColor());
Shape greenRectangle = new Rectangle(200, 100, new GreenColor());
redCircle.draw(); // Output: Drawing Circle at (100, 100), radius: 50, Applying red color
greenRectangle.draw(); // Output: Drawing Rectangle with width: 200, height: 100, Applying green color
}
}
In this example:
Shape
class is the abstraction that represents a shape. It contains a reference to the Color
interface, which is the implementor.Circle
and Rectangle
classes are the refined abstractions that extend the Shape
class. They implement the draw()
method and delegate the color-related operations to the Color
interface.Color
interface is the implementor that declares the applyColor()
method, which is implemented by concrete classes.RedColor
and GreenColor
classes are the concrete implementors that implement the Color
interface.Client
class demonstrates the usage of the bridge pattern. It creates instances of Circle
and Rectangle
, passing the respective Color
implementation. The draw()
method is called on each shape, and the color-related operations are performed by the respective Color
implementation.By using the Bridge pattern, you can decouple abstractions from their implementations, allowing them to vary independently. It promotes flexibility, extensibility, and separation of concerns. Changes in the abstraction or implementation do not affect each other, and new abstractions or implementations can be added without modifying the existing code.