Beispiel #1
0
        //单例模式扩展
        private static void CreateAppConfigExtend()
        {
            HashSet <AppConfigExtend> hashset = new HashSet <AppConfigExtend>();

            for (int i = 0; i < 10; i++)
            {
                hashset.Add(AppConfigExtend.GetInstance());
            }
            Console.WriteLine(hashset.Count());//print:3 即全局中AppConfigExtend就只有3个实例对象
            Console.ReadKey();
        }
        //类外方法访问实例的访问点
        public static AppConfigExtend GetInstance()
        {
            if (!dic.ContainsKey(key))
            {
                dic.Add(key, new AppConfigExtend());
            }
            AppConfigExtend appConfig = dic[key];

            key++;
            if (key > MaxInstance)
            {
                key = 1;
            }
            return(appConfig);
        }