C/C++教程

设计模式-》工厂Factory

本文主要是介绍设计模式-》工厂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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!