• 设计模式》构造者模式


    抽象构造

    AbsPlatformManagerBuilder,H5PlatformManagerBuilder,AppPlatformManagerBuilder,AbsPlatformManager,H5PlatformManager,AppPlatformManager,USAH5PlatformManager,USAAppPlatformManager,USAH5PlatformManagerBuilder,USAAppPlatformManagerBuilder

    点击查看代码
        public abstract class AbsPlatformManager
        {
            /// <summary>
            /// 消息发送完成回调
            /// </summary>
            protected Action<Guid, string> MessageComplete { get; }
    
            protected AbsPlatformManager(Action<Guid, string> messageComplete)
            {
                MessageComplete = messageComplete;
            }
    
            protected AbsPlatformManager()
            {
            }
    
            public virtual void SendUserMessage(Guid userId, string msg)
            {
                if (MessageComplete != null)
                {
                    MessageComplete(userId, msg);
                }
            }
        }
    
        public abstract class AbsPlatformManagerBuilder
        {
            protected Action<Guid, string> MessageComplete { get; private set; }
    
            public AbsPlatformManagerBuilder AddMessageCompleteEvent(Action<Guid, string> messageComplete)
            {
                this.MessageComplete = messageComplete;
                return this;
            }
    
    
            public abstract AbsPlatformManager Builder();
        }
        public class AppPlatformManager : AbsPlatformManager
        {
            public override void SendUserMessage(Guid userId, string msg)
            {
                Console.WriteLine($"App发送消息:{userId},{msg}");
                base.SendUserMessage(userId, msg);
            }
            public AppPlatformManager(Action<Guid, string> messageComplete):base(messageComplete)
            {
            }
    
            public AppPlatformManager()
            {
            }
        }
        public class AppPlatformManagerBuilder : AbsPlatformManagerBuilder
        {
            public override AbsPlatformManager Builder()
            {
                var appPlatformManager = new AppPlatformManager(this.MessageComplete);
                return appPlatformManager;
            }
        }
    
       public class H5PlatformManager : AbsPlatformManager
        {
            public override void SendUserMessage(Guid userId, string msg)
            {
                Console.WriteLine($"H5发送消息:{userId},{msg}");
                base.SendUserMessage(userId, msg);
            }
            
            public H5PlatformManager(Action<Guid, string> messageComplete):base(messageComplete)
            {
            }
    
            public H5PlatformManager()
            {
            }
        }
        public class H5PlatformManagerBuilder : AbsPlatformManagerBuilder
        {
            public override AbsPlatformManager Builder()
            {
                var h5PlatformManager = new H5PlatformManager(this.MessageComplete);
                return h5PlatformManager;
            }
        }
       public class USAAppPlatformManager : AppPlatformManager
        {
            public override void SendUserMessage(Guid userId, string msg)
            {
                Console.WriteLine($"来自美国APP 发送消息:{userId},{msg}");
                this.MessageComplete(userId, msg);
                // base.SendUserMessage(userId, msg);
            }
    
            public USAAppPlatformManager(Action<Guid, string> messageComplete) : base(messageComplete)
            {
            }
    
            public USAAppPlatformManager()
            {
            }
        }
       public class USAAppPlatformManagerBuilder : AppPlatformManagerBuilder
        {
            public override AbsPlatformManager Builder()
            {
                var appPlatformManager = new USAAppPlatformManager(this.MessageComplete);
                return appPlatformManager;
            }
        }
      public class USAH5PlatformManager : H5PlatformManager
        {
            public override void SendUserMessage(Guid userId, string msg)
            {
                Console.WriteLine($"来自美国H5发送消息:{userId},{msg}");
                base.SendUserMessage(userId, msg);
            }
    
            public USAH5PlatformManager(Action<Guid, string> messageComplete) : base(messageComplete)
            {
            }
    
            public USAH5PlatformManager()
            {
            }
        }
        public class USAH5PlatformManagerBuilder : H5PlatformManagerBuilder
        {
            public override AbsPlatformManager Builder()
            {
                var appPlatformManager = new USAH5PlatformManager(this.MessageComplete);
                return appPlatformManager;
            }
        }
    

    直接构造

    FriendManager,FriendManagerBuilder

        public class FriendManager
        {
            private Action _addFriendComplete;
    
            public FriendManager()
            {
            }
    
            public FriendManager(Action addFriendComplete)
            {
                _addFriendComplete = addFriendComplete;
            }
    
            public void AddFriend(Guid userId)
            {
                Console.WriteLine($"添加userId{userId}成功");
    
                if (_addFriendComplete != null)
                {
                    _addFriendComplete();
                }
            }
        }
    
        public class FriendManagerBuilder
        {
            private Action _addFriendComplete;
    
            public FriendManagerBuilder AddFriendCompleteEvent(Action action)
            {
                _addFriendComplete = action;
                return this;
            }
    
            public FriendManager Builder()
            {
                return new FriendManager(_addFriendComplete);
            }
        }
    

    Main

     class Program
        {
            static void Main(string[] args)
            {
                TestFactoryBuilder();
                // TestFriend();
            }
    
            private static void TestFriend()
            {
                FriendManager friendManager = new FriendManagerBuilder().AddFriendCompleteEvent(() =>
                {
                    Console.WriteLine("监听加好友成功回调");
                }).Builder();
                friendManager.AddFriend(Guid.NewGuid());
            }
    
            private static void TestFactoryBuilder()
            {
                AbsPlatformManager platformManager = new H5PlatformManagerBuilder().AddMessageCompleteEvent((userId,
                    message) =>
                {
                    Console.WriteLine($"H5监听发送完毕:{userId},{message}");
                }).Builder();
                Guid userId = Guid.NewGuid();
                string message = "message";
                platformManager.SendUserMessage(userId, message);
    
    
                AbsPlatformManager appPlatformManager = new AppPlatformManagerBuilder().AddMessageCompleteEvent((userId,
                    message) =>
                {
                    Console.WriteLine($"App监听发送完毕:{userId},{message}");
                }).Builder();
                appPlatformManager.SendUserMessage(userId, message);
    
    
                AbsPlatformManager UsaAppPlatformManager = new USAAppPlatformManagerBuilder().AddMessageCompleteEvent((
                    userId,
                    message) =>
                {
                    Console.WriteLine($"美国App监听发送完毕:{userId},{message}");
                }).Builder();
                UsaAppPlatformManager.SendUserMessage(userId, message);
            }
        }
    
  • 相关阅读:
    Spring笔记系列--1--基本内容与xml配置
    aop(面向切面编程)在Java中的两种动态代理实现
    linux配置网络,关闭防火墙命令操作
    Nginx知识记录
    发邮件工具类
    增强请求响应——代理模式
    取url中的参数值
    lumisoft会将eml后缀格式的附件给解析成文本,这里保存成文件
    lumisoft邮件头不规范造成内容无法读取
    lumisoft邮件内容中文乱码问题
  • 原文地址:https://www.cnblogs.com/icxldd/p/15796084.html
Copyright © 2020-2023  润新知