• 设计模式》桥接模式


    例子

    • 给用户在不同平台发送消息(微信,APP,H5)
        public abstract class AbsUserManager
        {
            protected AbsUserManager(AbsUserManagerProvider userManagerProvider)
            {
                UserManagerProvider = userManagerProvider;
            }
    
            protected AbsUserManagerProvider UserManagerProvider { get; }
    
            /// <summary>
            /// 发送消息
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="msg"></param>
            public void SendMessage(Guid userId, string msg)
            {
                UserManagerProvider.SendMessage(userId, msg);
            }
    
            /// <summary>
            /// 强制下线
            /// </summary>
            /// <param name="userId"></param>
            public void TapeOut(Guid userId)
            {
                UserManagerProvider.TapeOut(userId);
            }
        }
    
    
        public abstract class AbsUserManagerProvider
        {
            /// <summary>
            /// 发送消息
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="msg"></param>
            public abstract void SendMessage(Guid userId, string msg);
    
            /// <summary>
            /// 强制下线
            /// </summary>
            /// <param name="userId"></param>
            public abstract void TapeOut(Guid userId);
        }
    
        public class AppUserManager : AbsUserManager
        {
            public AppUserManager(AppUserManagerProvider userManagerProvider) : base(userManagerProvider)
            {
            }
        }
    
        public class AppUserManagerProvider : AbsUserManagerProvider
        {
            public override void SendMessage(Guid userId, string msg)
            {
                Console.WriteLine($"从APP向{userId.ToString()}发送消息:{msg}");
            }
    
            public override void TapeOut(Guid userId)
            {
                Console.WriteLine($"从APP把{userId.ToString()}踢下线");
            }
        }
    
    
        public class H5UserManager : AbsUserManager
        {
            public H5UserManager(H5UserManagerProvider userManagerProvider) : base(userManagerProvider)
            {
            }
        }
    
        public class H5UserManagerProvider : AbsUserManagerProvider
        {
            public override void SendMessage(Guid userId, string msg)
            {
                Console.WriteLine($"从H5向{userId.ToString()}发送消息:{msg}");
            }
    
            public override void TapeOut(Guid userId)
            {
                Console.WriteLine($"从H5把{userId.ToString()}踢下线");
            }
        }
    
        public class WechatUserManager : AbsUserManager
        {
            public WechatUserManager(WechatUserManagerProvider userManagerProvider) : base(userManagerProvider)
            {
            }
        }
    
        public class WechatUserManagerProvider : AbsUserManagerProvider
        {
            public override void SendMessage(Guid userId, string msg)
            {
                Console.WriteLine($"从微信向{userId.ToString()}发送消息:{msg}");
            }
    
            public override void TapeOut(Guid userId)
            {
                throw new NotImplementedException();
            }
        }
    
    
    
            static void Main(string[] args)
            {
                Guid userId = Guid.NewGuid();
                string msg = "123123";
    
                new H5UserManager(new H5UserManagerProvider()).SendMessage(userId, msg);
                new AppUserManager(new AppUserManagerProvider()).SendMessage(userId, msg);
                new WechatUserManager(new WechatUserManagerProvider()).SendMessage(userId, msg);
                
                
                new H5UserManager(new H5UserManagerProvider()).TapeOut(userId);
                new AppUserManager(new AppUserManagerProvider()).TapeOut(userId);
                new WechatUserManager(new WechatUserManagerProvider()).TapeOut(userId);
            }
    
    

    个人总结

    桥接模式就是 关联的属性是抽象类

    参考地址

  • 相关阅读:
    0/1背包问题
    假如爱有天意(中文版)
    tomcat集群
    分布式锁
    centos7安装Harbor(转载)
    isEmpty和isBlank的区别
    单体应用架构和分布式架构的比较
    微服务鉴权
    mysql的时区设置
    RSA非对称加密算法
  • 原文地址:https://www.cnblogs.com/icxldd/p/15799620.html
Copyright © 2020-2023  润新知