定义:

桥接模式:将抽象部分与它的实现部分分离,使它们都可以独立地变化。

解释:抽象与它的实现分离并不是说让抽象类与其派生类分离,而是抽象类和它的派生类用来实现自己的对象。

 

UML类图和基本代码:

设计模式(8)---桥接模式

class Program
    {
        static void Main(string[] args)
        {
            Abstration ab = new RefinedAbstration();

            ab.SetImplementor (new ConcreteImplementorA ());
            ab.Operation();

            ab.SetImplementor(new ConcreteImplementorB());
            ab.Operation();

            Console.Read();
        }
    }

    abstract class Implementor
    {
        public abstract void Operation();
    }

    class ConcreteImplementorA : Implementor
    {
        public override void Operation()
        {
            Console.WriteLine("implement A action");
        }
    }

    class ConcreteImplementorB : Implementor
    {
        public override void Operation()
        {
            Console.WriteLine("implement B action");
        }
    }

    class Abstration
    {
        protected Implementor implementor;

        public void SetImplementor(Implementor implementor)
        {
            this.implementor = implementor;
        }

        public virtual void Operation()
        {
            implementor.Operation();
        }
    }

    class RefinedAbstration : Abstration
    {
        public override void Operation()
        {
            implementor.Operation();
        }
    }
View Code

相关文章:

  • 2021-12-10
  • 2021-10-18
  • 2021-10-18
  • 2021-06-11
  • 2021-12-25
猜你喜欢
  • 2021-05-12
  • 2021-09-21
  • 2022-12-23
  • 2021-04-14
  • 2022-12-23
相关资源
相似解决方案