设计模式-Provider模式

Provider和Factory的区别:
可以往工厂里面添加Provider。也就是说Factory里面可能存在着许许多多的Provider。而这些Provider将是最后Factory创建出结果的必要支撑。
我们可以在项目中根据已有的provider接口,演变出各种的策略来,比如 XMLLogProvider、ConsoleLogProvider、JsonLogProvider等等。
但是它仅仅关注的是它要创建的细粒度对象,而不像工厂一样负责各个粒度对象的拼装,最终产生一个大粒度对象。

  /// <summary>
    /// 配置项
    /// </summary>
    public class ConfigSetting
    {
        public string Name { get; set; }
        public string DefaultValue { get; set; }

        public ConfigSetting(string name, string defaultValue)
        {
            this.Name = name;
            this.DefaultValue = defaultValue;
        }
    }
    /// <summary>
    /// 配置提供者,用来返回具体的配置项列表
    /// </summary>
    public abstract class SettingProvider
    {
        public abstract IEnumerable<ConfigSetting> GetSettingDefinitions();
    }
    /// <summary>
    /// 代码配置提供者
    /// </summary>
    public class CodeSettingProvider : SettingProvider
    {
        public override IEnumerable<ConfigSetting> GetSettingDefinitions()
        {
            return new List<ConfigSetting> {
            new ConfigSetting("Author.UserName","fan"),
            new ConfigSetting("Author.Age","18"),
            new ConfigSetting("Author.QQ","410577910"),
            };
        }
    }
    /// <summary>
    /// Xml配置提供者
    /// </summary>
    public class XmlSettingProvider : SettingProvider
    {
        static XmlSettingProvider()
        { 
            /*读取xml文件*/
        }
        public override IEnumerable<ConfigSetting> GetSettingDefinitions()
        {
            return new List<ConfigSetting> {/*从xml文件中获取配置,略...*/};
        }
    }
    /// <summary>
    /// 提供者集合
    /// </summary>
    public class ProviderCollection
    { 
         public List<Type> Providers { get; private set; }
        public ProviderCollection()
        {
            this.Providers = new List<Type>();
        }
        public void AddProvider<T>()
        {
            if (typeof(SettingProvider).IsAssignableFrom(typeof(T)))
            {
                this.Providers.Add(typeof(T));
            }
        }
    }
    /// <summary>
    /// 读取配置接口
    /// </summary>
    public interface IConfigSettingManager
    {
        /// <summary>
        /// 通过名称获取配置项
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        ConfigSetting GetSettingDefinition(string name);
        /// <summary>
        /// 获取全部配置项
        /// </summary>
        /// <returns></returns>
        IEnumerable<ConfigSetting> GetAllConfigSettings();
    }
    public class ConfigSettingManager : IConfigSettingManager
    {
        private readonly ProviderCollection _providerCollection = null;
        private readonly Dictionary<string, ConfigSetting> _configSettings = null;
        public ConfigSettingManager(ProviderCollection providerCollection)
        {
            this._providerCollection = providerCollection;
            this._configSettings = new Dictionary<string, ConfigSetting>();
        }
        public void Initialize()
        {
            foreach (var providerType in this._providerCollection.Providers)
            {
                if (Activator.CreateInstance(providerType) is SettingProvider provider)
                {
                    foreach (var configSetting in provider.GetSettingDefinitions())
                    {
                        this._configSettings.Add(configSetting.Name, configSetting);
                    }
                }
            }
        }
        /// <summary>
        /// 获取所有配置
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ConfigSetting> GetAllConfigSettings()
        {
            return _configSettings.Values;
        }
        /// <summary>
        /// 通过名称获取配置项
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ConfigSetting GetSettingDefinition(string name)
        {
            return this._configSettings[name];
        }
    }
posted @ 2020-08-11 00:35  .Neterr  阅读(2362)  评论(0编辑  收藏  举报