设计模式-》构造者模式
2022/1/13 6:03:59
本文主要是介绍设计模式-》构造者模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
抽象构造
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); } }
这篇关于设计模式-》构造者模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-11cursor试用出现:Too many free trial accounts used on this machine 的解决方法
- 2025-01-11百万架构师第十四课:源码分析:Spring 源码分析:深入分析IOC那些鲜为人知的细节|JavaGuide
- 2025-01-11不得不了解的高效AI办公工具API
- 2025-01-102025 蛇年,J 人直播带货内容审核团队必备的办公软件有哪 6 款?
- 2025-01-10高效运营背后的支柱:文档管理优化指南
- 2025-01-10年末压力山大?试试优化你的文档管理
- 2025-01-10跨部门协作中的进度追踪重要性解析
- 2025-01-10总结 JavaScript 中的变体函数调用方式
- 2025-01-10HR团队如何通过数据驱动提升管理效率?6个策略
- 2025-01-10WBS实战指南:如何一步步构建高效项目管理框架?