zoukankan      html  css  js  c++  java
  • 提供者模式(Provider patten)

      /// <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];
            }
        }
    
  • 相关阅读:
    大数据项目实战之在线教育(01数仓需求)
    大数据项目实战之在线教育(02数仓实现)
    大数据之flink教程-TableAPI和SQL
    尚硅谷大数据技术之电商用户行为数据分析
    大数据实时项目(采集部分)
    大数据实时项目(交易额)
    作业一
    预备作业
    重建二叉树
    矩形覆盖
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/13473173.html
Copyright © 2011-2022 走看看