桥接模式是一种结构型设计模式,它将抽象和实现分离,使它们可以独立地变化。通过使用桥接模式,可以将一个类的抽象部分与其具体实现部分解耦,并且可以在运行时动态地选择不同的实现。
抽象化(Abstraction)角色通过一个成员变量持有具体实施者(Concrete Implementor)角色的引用。这个成员变量充当了抽象化角色和具体实施者角色之间的桥梁。通过将抽象化角色和具体实施者角色解耦,使得它们可以独立地进行扩展和修改。
- // 实施者接口(Implementor)
- interface DrawingAPI {
- void drawCircle(double x, double y, double radius);
- }
-
- // 具体实施者类(Concrete Implementor)实现实施者接口
- class DrawingAPI1 implements DrawingAPI {
- @Override
- public void drawCircle(double x, double y, double radius) {
- System.out.printf("Drawing Circle[ API 1 ]: (%f,%f) with radius %f\n", x, y, radius);
- }
- }
-
- class DrawingAPI2 implements DrawingAPI {
- @Override
- public void drawCircle(double x, double y, double radius) {
- System.out.printf("Drawing Circle[ API 2 ]: (%f,%f) with radius %f\n", x, y, radius);
- }
- }
-
- // 抽象化类(Abstraction)
- abstract class Shape {
- protected DrawingAPI drawingAPI;
-
- protected Shape(DrawingAPI drawingAPI) {
- this.drawingAPI = drawingAPI;
- }
-
- public abstract void draw();
- }
-
- // 扩展抽象化类(Refined Abstraction)
- class CircleShape extends Shape {
- private double x, y, radius;
-
- public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) {
- super(drawingAPI);
- this.x = x;
- this.y = y;
- this.radius = radius;
- }
-
- @Override
- public void draw() {
- drawingAPI.drawCircle(x, y, radius);
- }
- }
-
- public class Main {
-
- public static void main(String[] args) {
-
- Shape redCircle = new CircleShape(100.0f, 100.0f, 10.0f,
- new DrawingAPI1());
- Shape greenCircle = new CircleShape(200.0f, 200.0f, 20.0f,
- new DrawingAPI1());
-
- redCircle.draw();
- greenCircle.draw();
- }
- }
在上述示例中,我们定义了一个接口 DrawingApi 来表示实施者的行为,并有两个具体实现类 DrawingApi1 和 DrawingApi2。然后,我们创建了一个抽象类 Shape,它包含一个成员变量 drawingApi 来持有具体实施者的引用,并提供了一个抽象方法 draw()。最后,我们创建了扩展抽象化的子类 CircleShape,它通过调用 drawingApi 的方法来实现具体的绘制逻辑。
在主函数中,我们创建了两个不同颜色的圆形对象,并调用它们的 draw() 方法来进行绘制。由于抽象化角色和具体实施者角色解耦,我们可以轻松地切换不同的具体实施者类来改变绘制效果。
存在问题: