• 设计模式》桥接模式


    例子

    • 给用户在不同平台发送消息(微信,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);
            }
    
    

    个人总结

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

    参考地址

  • 相关阅读:
    TCP和UDP的最完整的区别
    kafka重置到最新offset偏移量
    MYSQL中IN,INSTR,FIND_IN_SET函数效率比较
    本地不安装ORACLE,用PLSQL访问远程数据库
    MySQL中的DEFINER与SQL SECURITY
    Centos6.8 安装tomcat8.5.11
    动态代理模式
    linux下mysql允许远程连接
    全面理解Java中的String数据类型
    Spring中获取web项目的根目录
  • 原文地址:https://www.cnblogs.com/icxldd/p/15799620.html
Copyright © 2020-2023  润新知