设计模式之桥接模式(Bridge)

桥接模式定义

桥接模式:解决类的多维度变化
将类的爆炸个数由M * N * L降为M+N+L个
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码实现

手机沿着操作系统和品牌两个维度发生了变化

    public interface ISystem
    {
        string System();
        string Version();
    }
    public class AndroidSystem : ISystem
    {
       public string System()
        {
            return "Android";
        }
        public string Version()
        {
            return "6.0";
        }
    }
    public class IOSSystem : ISystem
    {
        public string System()
        {
            return "IOS";
        }
        public string Version()
        {
            return "9.4";
        }
    }
    public class WinphoneSystem : ISystem
    {
        public string System()
        {
            return "Winphone";
        }
        public string Version()
        {
            return "10.0";
        }
    }

以上是系统抽象类和具体的系统实现类。

    /// <summary>
    /// 抽象父类
    /// </summary>
    public abstract class BasePhoneBridge
    {
        public ISystem SystemVersion { get; set; }
        /// <summary>
        /// 打电话
        /// </summary>
        public abstract void Call();
        /// <summary>
        /// 发短信
        /// </summary>
        public abstract void Text();
    }
    public class GalaxyBridge : BasePhoneBridge
    {
        public override void Call()
        {
            Console.WriteLine("Use OS {0}.{1}.{2} Call", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
        }
        public override void Text()
        {
            Console.WriteLine("Use OS {0}.{1}.{2} Text", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
        }
    }
    public class LumiaBridge : BasePhoneBridge
    {
        public override void Call()
        {
            Console.WriteLine("Use OS {0}.{1}.{2} Call", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
        }
        public override void Text()
        {
            Console.WriteLine("Use OS {0}.{1}.{2} Text", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
        }
    }
    public class iPhoneBridge : BasePhoneBridge
    {
        public override void Call()
        {
            Console.WriteLine("Use OS {0}.{1}.{2} Call", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
        }
        public override void Text()
        {
            Console.WriteLine("Use OS {0}.{1}.{2} Text", this.GetType().Name, this.SystemVersion.System(), this.SystemVersion.Version());
        }
    }

调用方代码如下:

            ISystem android = new AndroidSystem();
            ISystem ios = new IOSSystem();
            ISystem winphone = new WinphoneSystem();

            {
                BasePhoneBridge phone = new GalaxyBridge();
                phone.SystemVersion = android;
                phone.Call();
                phone.Text();
            }
            {
                BasePhoneBridge phone = new GalaxyBridge();
                phone.SystemVersion = ios;
                phone.Call();
                phone.Text();
            }
            {
                BasePhoneBridge phone = new GalaxyBridge();
                phone.SystemVersion = winphone;
                phone.Call();
                phone.Text();
            }

要点总结

在这里插入图片描述
桥接模式大大降低了类膨胀的个数,但是会让调用方变得复杂一些,需要对类的细节了解的更多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值