设计模式(分类) 设计模式(六大原则)
创建型(5种) 工厂方法 抽象工厂模式 单例模式 建造者模式 原型模式
结构型(7种) 适配器模式 装饰器模式 代理模式 外观模式 桥接模式 组合模式 享元模式
行为型(11种) 策略模式 模板方法模式 观察者模式 迭代器模式 责任链模式 命令模式
备忘录模式 状态模式 访问者模式 中介者模式
桥接模式(Bridge Pattern)是一种结构型设计模式,它将抽象部分与其实现部分分离,使它们可以独立地变化。桥接模式通过引入桥接接口,使得抽象类和实现类可以分别独立扩展,从而解决了多维度变化的问题。这种模式特别适用于那些接口部分和实现部分都需要独立扩展的场景。
模式结构
桥接模式通常包含以下角色:
工作原理
优缺点
优点
缺点
适用场景
代码示例(以Java为例)
// 实现接口
interface DrawingAPI {
void drawCircle(double x, double y, double radius);
}
// 具体实现类
class DrawingAPI1 implements DrawingAPI {
@Override
public void drawCircle(double x, double y, double radius) {
System.out.println("DrawingAPI1 drawing a circle at (" + x + ", " + y + ") radius " + radius);
}
}
class DrawingAPI2 implements DrawingAPI {
@Override
public void drawCircle(double x, double y, double radius) {
System.out.println("DrawingAPI2 drawing a circle at (" + x + ", " + y + ") radius " + radius);
}
}
// 抽象类
abstract class Shape {
protected DrawingAPI drawingAPI;
protected Shape(DrawingAPI drawingAPI) {
this.drawingAPI = drawingAPI;
}
abstract void draw();
}
// 具体抽象类
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
void draw() {
drawingAPI.drawCircle(x, y, radius);
}
}
// 客户端代码
public class BridgePatternDemo {
public static void main(String[] args) {
Shape circle1 = new CircleShape(1, 2, 3, new DrawingAPI1());
circle1.draw(); // 输出:DrawingAPI1 drawing a circle at (1.0, 2.0) radius 3.0
Shape circle2 = new CircleShape(4, 5, 6, new DrawingAPI2());
circle2.draw(); // 输出:DrawingAPI2 drawing a circle at (4.0, 5.0) radius 6.0
}
}
在这个Java示例中:
代码示例(以Python为例)
# 实现接口(Implementor)
class RenderingEngine:
"""渲染引擎接口"""
def render_circle(self, x, y, radius):
raise NotImplementedError()
def render_rectangle(self, x, y, width, height):
raise NotImplementedError()
# 具体实现类(Concrete Implementor)
class SketchRenderingEngine(RenderingEngine):
"""素描风格渲染引擎"""
def render_circle(self, x, y, radius):
print(f"Sketch rendering engine is drawing a circle at ({x}, {y}) with radius {radius}.")
def render_rectangle(self, x, y, width, height):
print(f"Sketch rendering engine is drawing a rectangle at ({x}, {y}) with dimensions ({width}, {height}).")
class WatercolorRenderingEngine(RenderingEngine):
"""水彩风格渲染引擎"""
def render_circle(self, x, y, radius):
print(f"Watercolor rendering engine is painting a circle at ({x}, {y}) with radius {radius}.")
def render_rectangle(self, x, y, width, height):
print(f"Watercolor rendering engine is painting a rectangle at ({x}, {y}) with dimensions ({width}, {height}).")
# 抽象类(Abstraction)
class Shape:
"""图形抽象类"""
def __init__(self, rendering_engine):
self.rendering_engine = rendering_engine
def draw(self):
raise NotImplementedError()
# 具体抽象类(Concrete Abstraction)
class Circle(Shape):
"""圆形"""
def __init__(self, x, y, radius, rendering_engine):
super().__init__(rendering_engine)
self.x = x
self.y = y
self.radius = radius
def draw(self):
self.rendering_engine.render_circle(self.x, self.y, self.radius)
class Rectangle(Shape):
"""矩形"""
def __init__(self, x, y, width, height, rendering_engine):
super().__init__(rendering_engine)
self.x = x
self.y = y
self.width = width
self.height = height
def draw(self):
self.rendering_engine.render_rectangle(self.x, self.y, self.width, self.height)
# 客户端代码
if __name__ == "__main__":
# 创建图形与渲染引擎
circle_sketch = Circle(1, 2, 3, SketchRenderingEngine())
circle_watercolor = Circle(4, 5, 6, WatercolorRenderingEngine())
rect_sketch = Rectangle(7, 8, 9, 10, SketchRenderingEngine())
rect_watercolor = Rectangle(11, 12, 13, 14, WatercolorRenderingEngine())
# 绘制图形
circle_sketch.draw()
circle_watercolor.draw()
rect_sketch.draw()
rect_watercolor.draw()
在这个例子中,我们将模拟一个图形系统,它允许用户绘制不同形状(如圆形、矩形),同时支持多种渲染引擎(如素描风格、水彩风格)。桥接模式在这里帮助我们将图形的抽象定义(形状)与具体的渲染实现(渲染引擎)分离,使得这两部分可以独立扩展。
在这个Python示例中: