桥接模式
The Bridge design pattern decouples an abstraction from its implementation so tha the two can vary independently.
桥接模式将抽象和实现解耦,以便两者可以独立变化。
UML Class Diagram
Abstraction: defines the abstraction's interface;matains a reference to an object of type implementor
RefinedAbstraction: extends the interface defined by Abstraction.
Implementor: defines the interface for implementation classes. This interface doesn't have to correspond exactly to Abstraction's interface;in fact two interfaces can be quite different.Typically the implementation interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives.
ConcreteImplementor: implements the implementor interface and defines its concrete implementation.
Structure Code in C#

/// <summary> /// This is an abstract class that contains members that define an abstract business object and its funcationality. /// It contains a reference to an object of type IImplementor and delegates all of the rea work to this object. /// It can also act as the base class for other abstraction. /// </summary> public class Abstraction { protected IImplementor? implementor; public IImplementor Implementor { set { implementor = value; } } public virtual void Operation() { implementor?.Operation(); } } /// <summary> /// This is going to be a concrete class which form the Abstraction class. /// This Redefined Abstraction class extends the interface defined by Abstraction class. /// </summary> public class RefinedAbstraction : Abstraction { public override void Operation() { implementor?.Operation(); } }

/// <summary> /// This is going to be an interface that acts as a bridge between the abstraction classes and implementer classes. /// The Implementor interface defines the operations for all implementation classes. /// It doesn't have to match the Abstraction's interface. /// In fact, the two interfaces can be entirely different. /// </summary> public interface IImplementor { void Operation(); } /// <summary> /// This is going to be a class which implements the IImplementor interface and also provide the implementation details for the associated Abstraction class. /// Each Concrete implementation corresponds to a specific platform. /// </summary> public class ConcreteImplementor : IImplementor { public void Operation() { Console.WriteLine("ConcreteImplementor operation"); } }
When do we need to use Bridge Design Pattern in C#?
- we want to hide the implementation details from the client.
- we want to selection or switching of the implemtation to be at runtime rather than desgin time.
- we want both the abstration and implenmentation classes to be extensible by the subclasses.
- we want to avoid tight coupling binding between an abstraction and its implementation.
- The changes in the implementation of an abtraction should have no impact on clients.
- when we want to share an implementation among multiple objects and this should be hidden from the client.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)