New Recording 3.m4a

UML

image.png

UML Example

image.png

Example

// Implementor interface
interface Color {
    void applyColor();
}

// Concrete Implementors
class RedColor implements Color {
    @Override
    public void applyColor() {
        System.out.println("Applying red color.");
    }
}

class BlueColor implements Color {
    @Override
    public void applyColor() {
        System.out.println("Applying blue color.");
    }
}

// Abstraction
abstract class Shape {
    protected Color color;

    // Constructor to bind a Color to a Shape
    protected Shape(Color color) {
        this.color = color;
    }

    abstract void draw();
}

// Refined Abstraction 1
class Circle extends Shape {
    private int radius;

    public Circle(int radius, Color color) {
        super(color);
        this.radius = radius;
    }

    @Override
    void draw() {
        System.out.print("Drawing Circle with radius " + radius + ". ");
        color.applyColor();
    }
}

// Refined Abstraction 2
class Rectangle extends Shape {
    private int width, height;

    public Rectangle(int width, int height, Color color) {
        super(color);
        this.width = width;
        this.height = height;
    }

    @Override
    void draw() {
        System.out.print("Drawing Rectangle with dimensions " + width + "x" + height + ". ");
        color.applyColor();
    }
}

// Client code
public class BridgePatternExample {
    public static void main(String[] args) {
        // Create objects with different shape-color combinations
        Shape redCircle = new Circle(10, new RedColor());
        Shape blueRectangle = new Rectangle(20, 30, new BlueColor());

        // Draw shapes
        redCircle.draw();
        blueRectangle.draw();
    }
}