0
点赞
收藏
分享

微信扫一扫

Java 桥接模式

桥接模式简介 桥接模式(Bridge Pattern)是一种结构型设计模式,用于将抽象部分与实现部分分离,使它们可以独立变化。这种模式特别适用于以下场景: 当一个类存在多种变体,而这些变体之间又存在多个维度的变化时。 当不希望在抽象和实现之间有固定的绑定关系时。 桥接模式的结构 桥接模式主要涉及以下几个角色: Abstraction(抽象类):定义抽象类的接口,并维护一个对实现部分的引用。 RefinedAbstraction(扩展抽象类):扩展抽象类,实现更具体的业务逻辑。 Implementor(实现接口):定义实现部分的接口。 ConcreteImplementor(具体实现类):实现实现接口,提供具体的实现。 示例代码 假设我们有一个绘图应用,需要支持不同的图形(圆形、矩形)和不同的绘制方式(普通绘制、高亮绘制)。我们可以使用桥接模式来实现这一需求。

  1. 实现接口(Implementor)

public interface DrawAPI {
    void drawCircle(int radius, int x, int y);
    void drawRectangle(int width, int height, int x, int y);
}

  1. 具体实现类(ConcreteImplementor)

public class NormalDrawAPI implements DrawAPI {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle [color: normal, radius: " + radius + ", x: " + x + ", y: " + y + "]");
    }

    @Override
    public void drawRectangle(int width, int height, int x, int y) {
        System.out.println("Drawing Rectangle [color: normal, width: " + width + ", height: " + height + ", x: " + x + ", y: " + y + "]");
    }
}

public class HighlightDrawAPI implements DrawAPI {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle [color: highlight, radius: " + radius + ", x: " + x + ", y: " + y + "]");
    }

    @Override
    public void drawRectangle(int width, int height, int x, int y) {
        System.out.println("Drawing Rectangle [color: highlight, width: " + width + ", height: " + height + ", x: " + x + ", y: " + y + "]");
    }
}

  1. 抽象类(Abstraction)

public abstract class Shape {
    protected DrawAPI drawAPI;

    protected Shape(DrawAPI drawAPI) {
        this.drawAPI = drawAPI;
    }

    public abstract void draw();
}

  1. 扩展抽象类(RefinedAbstraction)

public class Circle extends Shape {
    private int x, y, radius;

    public Circle(int x, int y, int radius, DrawAPI drawAPI) {
        super(drawAPI);
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    @Override
    public void draw() {
        drawAPI.drawCircle(radius, x, y);
    }
}

public class Rectangle extends Shape {
    private int x, y, width, height;

    public Rectangle(int x, int y, int width, int height, DrawAPI drawAPI) {
        super(drawAPI);
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    @Override
    public void draw() {
        drawAPI.drawRectangle(width, height, x, y);
    }
}

  1. 客户端代码

public class BridgePatternDemo {
    public static void main(String[] args) {
        Shape circle1 = new Circle(10, 10, 5, new NormalDrawAPI());
        Shape circle2 = new Circle(10, 10, 5, new HighlightDrawAPI());

        Shape rectangle1 = new Rectangle(10, 10, 15, 10, new NormalDrawAPI());
        Shape rectangle2 = new Rectangle(10, 10, 15, 10, new HighlightDrawAPI());

        circle1.draw();
        circle2.draw();

        rectangle1.draw();
        rectangle2.draw();
    }
}

总结

通过桥接模式,我们可以将抽象部分与实现部分分离,使得两者可以独立变化。这不仅提高了系统的灵活性,还减少了类的总数,降低了系统的复杂度。在实际开发中,桥接模式常用于处理多维度变化的场景,如图形绘制、数据库访问等。

举报

相关推荐

0 条评论