Java教程

设计模式-》构造者模式

本文主要是介绍设计模式-》构造者模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

抽象构造

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);
        }
    }
这篇关于设计模式-》构造者模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!