设计模式 7 桥接模式

设计模式 7

  • 创建型模式(5):工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式
  • 结构型模式(7):适配器模式、桥接模式、组合模式、装饰者模式、外观模式、享元模式、代理模式
  • 行为型模式(11):责任链模式、命令模式、解释器模式、迭代器模式、中介者模式、备忘录模式、观察者模式、状态模式、策略模式、模板方法模式、访问者模式

文章目录

  • 设计模式 7
    • 桥接模式(Bridge Pattern)
      • 1 定义
      • 2 结构
      • 3 示例代码
      • 4 特点
      • 5 适用场景
      • 6 与其他模式的关系

桥接模式(Bridge Pattern)

1 定义

桥接模式允许在运行时将抽象类与其实现类解耦。它通过引入一个接口来使得抽象类与具体的实现类分开,从而支持两者的独立变化。这样可以避免复杂的多层继承结构。

2 结构

桥接模式的结构包含以下角色:

  • 抽象类(Abstraction): 定义抽象接口,并保存一个指向实现类的引用。
  • 扩展抽象类(Refined Abstraction): 继承抽象类,并实现其接口。
  • 实现类接口(Implementor): 定义实现类的接口。
  • 具体实现类(Concrete Implementor): 实现实现类接口的具体类。

UML 类图

+-------------------+        +-------------------+
|    Abstraction    |        |   Implementor     |
+-------------------+        +-------------------+
| - implementor     |        | + OperationImpl() |
| + Operation()     |        +-------------------+
+-------------------+                 ^
        |                             |
        |                             |
+-------------------+        +---------------------+
| RefinedAbstraction|        | ConcreteImplementor |
+-------------------+        +---------------------+
| + Operation()     |        | + OperationImpl()   |
+-------------------+        +---------------------+

3 示例代码

以下是一个实现桥接模式的简单示例。假设我们要创建一个图形绘制程序,其中有不同类型的图形和不同的绘制方式。

实现类接口

// 实现者接口
public interface IColor
{
    void ApplyColor();
}

具体实现类

// 具体实现者1:红色
public class RedColor : IColor
{
    public void ApplyColor()
    {
        Console.WriteLine("Applying red color.");
    }
}

// 具体实现者2:绿色
public class GreenColor : IColor
{
    public void ApplyColor()
    {
        Console.WriteLine("Applying green color.");
    }
}

抽象类

// 抽象类:形状
public abstract class Shape
{
    protected IColor color;

    protected Shape(IColor color)
    {
        this.color = color;
    }

    public abstract void Draw();
}

扩展抽象类

// 细化抽象类1:圆形
public class Circle : Shape
{
    public Circle(IColor color) : base(color)
    {
    }

    public override void Draw()
    {
        Console.Write("Circle is being drawn. ");
        color.ApplyColor();
    }
}

// 细化抽象类2:矩形
public class Rectangle : Shape
{
    public Rectangle(IColor color) : base(color)
    {
    }

    public override void Draw()
    {
        Console.Write("Rectangle is being drawn. ");
        color.ApplyColor();
    }
}

客户端代码

class Program
{
    static void Main(string[] args)
    {
        // 创建红色和绿色的实现者
        IColor red = new RedColor();
        IColor green = new GreenColor();

        // 创建带有不同颜色的形状
        Shape redCircle = new Circle(red);
        Shape greenRectangle = new Rectangle(green);

        // 画形状
        redCircle.Draw();  // 输出: Circle is being drawn. Applying red color.
        greenRectangle.Draw();  // 输出: Rectangle is being drawn. Applying green color.
    }
}

在这个例子中:

  • IColor 是实现者接口,定义了 ApplyColor() 方法。
  • RedColorGreenColor 是具体实现者,实现了 IColor 接口。
  • Shape 是抽象类,它维护了一个 IColor 类型的引用,并定义了 Draw() 方法。
  • CircleRectangleShape 的细化抽象类,分别实现了 Draw() 方法。

4 特点

  • 优点:

    • 解耦抽象与实现: 通过桥接模式,抽象与实现可以独立变化,降低了系统的耦合度。

    • 灵活性: 可以很方便地扩展新的抽象和实现类,而不需要修改已有的代码。

    • 符合开闭原则: 新的实现可以在不修改原有代码的情况下添加。

  • 缺点:

    • 增加了系统的复杂性: 由于引入了多个类和接口,可能会使得系统结构变得复杂。

    • 维护难度增加: 当扩展的类和接口数量增多时,维护这些类和接口的难度可能会增加。

5 适用场景

  • 多变的实现类: 当你有多个实现类和多个抽象类,并且你希望在运行时动态组合这些实现时,可以使用桥接模式。
  • 避免类爆炸: 当抽象类和实现类的数量不断增加,导致类的数量爆炸时,可以考虑使用桥接模式来减少复杂性。

6 与其他模式的关系

  • 适配器模式: 适配器模式用于解决接口不兼容的问题,而桥接模式用于将抽象与实现解耦。
  • 组合模式: 桥接模式与组合模式都使用了组合的方式来构建对象,但桥接模式侧重于解耦,而组合模式更关注于树形结构的创建。

桥接模式通过将抽象与实现分离,使得系统的扩展更加灵活,适用于需要同时支持多种变化的场景。

你可能感兴趣的:(#,设计模式,设计模式,桥接模式)