设计模式-》工厂Factory
2022/1/13 6:04:03
本文主要是介绍设计模式-》工厂Factory,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
简单工厂
DBProvider
点击查看代码
public interface IDbProvider { /// <summary> /// 获取db客户端 /// </summary> /// <returns></returns> void PrintDbClient(); } public class MysqlDbProvider : IDbProvider { public void PrintDbClient() { Console.WriteLine("我是mysql 提供者"); } } public class PgsqlDbProvider : IDbProvider { public void PrintDbClient() { Console.WriteLine("我是Pgsql 提供者"); } } public class SqlserverDbProvider : IDbProvider { public void PrintDbClient() { Console.WriteLine("我是Sqlserver 提供者"); } } public enum DbType { Mysql = 1, Sqlserver = 2, Pgsql = 3 } public static class DbProviderFactory { public static IDbProvider CreateDbProvider(DbType dbType) { IDbProvider provider = null; switch (dbType) { case DbType.Mysql: provider = new MysqlDbProvider(); break; case DbType.Pgsql: provider = new PgsqlDbProvider(); break; case DbType.Sqlserver: provider = new SqlserverDbProvider(); break; default: break; } return provider; } }
工厂方法
发消息Provider。分别有email,sms,JiGuang Provider。
点击查看代码
public interface IMessageProvider { void SendMessage(Guid userId, string message); } public abstract class AbsFactory { public abstract IMessageProvider CreateMessageProvider(); } public class EmailFactory : AbsFactory { public override IMessageProvider CreateMessageProvider() { return new EmailMessageProvider(); } } public class EmailMessageProvider : IMessageProvider { public void SendMessage(Guid userId, string message) { Console.WriteLine($"邮件发送: {userId.ToString()} : {message}"); } } public class JiGuangFactory : AbsFactory { public override IMessageProvider CreateMessageProvider() { return new JiGuangMessageProvider(); } } public class JiGuangMessageProvider : IMessageProvider { public void SendMessage(Guid userId, string message) { Console.WriteLine($"极光发送:{userId.ToString()}:{message}"); } } public class SmsFactory : AbsFactory { public override IMessageProvider CreateMessageProvider() { return new SmsMessageProvider(); } } public class SmsMessageProvider : IMessageProvider { public void SendMessage(Guid userId, string message) { Console.WriteLine($"短信发送: {userId.ToString()} : {message}"); } } public class FactoryMethodMessageProviderUnitTest : AbsUnitTest { public override void Test() { while (true) { Console.WriteLine("请输入1|2|3"); int type = Convert.ToInt32(Console.ReadLine()); Guid userId = Guid.NewGuid(); string message = "你好"; switch (type) { case 1: AbsFactory jiGuangFactory = new JiGuangFactory(); jiGuangFactory.CreateMessageProvider().SendMessage(userId, message); break; case 2: AbsFactory smsFactory = new SmsFactory(); smsFactory.CreateMessageProvider().SendMessage(userId, message); break; case 3: AbsFactory emailFactory = new EmailFactory(); emailFactory.CreateMessageProvider().SendMessage(userId, message); break; } } } }
抽象方法
分app,h5两个工厂(还可以增加微信小程序工厂去统计微信小程序的相关行为) 工厂提供行为:给用户发消息,统计当前平台的用户数据
点击查看代码
public abstract class AbsPlatformProviderFactory { public abstract IStatCalculate CreateStatCalculateProvider(); public abstract IPushMessage CreatePushMessageProvider(); } public interface IStatCalculate { void CalcTodayData(); } public interface IPushMessage { void PushMessage(Guid userId); } public class AppStatCalculate : IStatCalculate { public void CalcTodayData() { Console.WriteLine($"App:统计今日数据完成"); } } public class AppPushMessage : IPushMessage { public void PushMessage(Guid userId) { Console.WriteLine("App:发送消息完成" + userId.ToString()); } } public class AppPlatformProviderFactory : AbsPlatformProviderFactory { public override IStatCalculate CreateStatCalculateProvider() { return new AppStatCalculate(); } public override IPushMessage CreatePushMessageProvider() { return new AppPushMessage(); } } public class H5PushMessage : IPushMessage { public void PushMessage(Guid userId) { Console.WriteLine("H5:发送消息完成" + userId.ToString()); } } public class H5StatCalculate : IStatCalculate { public void CalcTodayData() { Console.WriteLine($"H5:统计今日数据完成"); } } public class H5PlatformProviderFactory : AbsPlatformProviderFactory { public override IStatCalculate CreateStatCalculateProvider() { return new H5StatCalculate(); } public override IPushMessage CreatePushMessageProvider() { return new H5PushMessage(); } } public class AbsFactoryUnitTest : AbsUnitTest { public override void Test() { while (true) { Console.WriteLine("请输入1|2"); int type = Convert.ToInt32(Console.ReadLine()); Guid userId = Guid.NewGuid(); string message = "你好"; switch (type) { case 1: AbsPlatformProviderFactory h5PlatformProviderFactory = new H5PlatformProviderFactory(); h5PlatformProviderFactory.CreatePushMessageProvider().PushMessage(userId); h5PlatformProviderFactory.CreateStatCalculateProvider().CalcTodayData(); break; case 2: AbsPlatformProviderFactory appPlatformProviderFactory = new AppPlatformProviderFactory(); appPlatformProviderFactory.CreatePushMessageProvider().PushMessage(userId); appPlatformProviderFactory.CreateStatCalculateProvider().CalcTodayData(); break; } } } }
这篇关于设计模式-》工厂Factory的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-10Rakuten 乐天积分系统从 Cassandra 到 TiDB 的选型与实战
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用