桥接模式

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();
        }
    }
Abstraction
/// <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");
        }
    }
Implementation

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.

 

posted @ 2023-06-03 21:34  云霄宇霁  阅读(2)  评论(0编辑  收藏  举报