c#设计模式——桥接模式

一 概要

1.1 结构型模式

  1. 关注类和对象的组合。继承的概念被用来组合接口和定义组合对象,从而获得新功能。

1.2 定义

  1. 将抽象与实现分离,使它们可以独立变化。它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度。

二 UML类图

在这里插入图片描述

三 例子

    class Program
    {
        static void Main(string[] args)
        {
            Abstraction abstraction = new AbstractionA();
            abstraction.SetImplementor(new ImplementorA());
            abstraction.Operation();
        }
      
    }

    public interface Implementor
    {
        void OperationImp();
    }

    public abstract class Abstraction
    {
        protected Implementor implementor;

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

        public abstract void Operation();
    }

    public class ImplementorA : Implementor
    {
        public void OperationImp()
        {
            //todo
        }
    }

    public class ImplementorB : Implementor
    {
        public void OperationImp()
        {
            //todo
        }
    }

    public class AbstractionA : Abstraction
    {
        public override void Operation()
        {
            if (this.implementor == null) return;
            this.implementor.OperationImp();
        }
    }

四 优缺点

4.1 优点

  1. 将抽象部分和它的实现部分分离, 使它们都可以独立地变化。并且各自的变化不会影响其他独立出来的模块的实现。
  2. 符合依赖倒转原则以及里氏代换原则。
  3. 低耦合。

4.2 缺点

  1. 增加系统复杂度。

五 使用场景

  1. 当一个类存在两个独立变化的维度,且这两个维度都需要进行扩展时。
  2. 当一个系统不希望使用继承或因为多层次继承导致系统类的个数急剧增加时。
  3. 当一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性时。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值