⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 来自DoFactory的设计模式, 由于是本人根据已有代码创建,没有NETOptimized项
💻 CS
字号:
// Singleton pattern -- Structural example  

using System;

namespace DoFactory.GangOfFour.Singleton.Structural
{

    // MainApp test application 

    class MainApp
    {

        static void Main()
        {
            // Constructor is protected -- cannot use new 
            Singleton s1 = Singleton.Instance();
            Singleton s2 = Singleton.Instance();

            if (s1 == s2)
            {
                Console.WriteLine("Objects are the same instance");
            }

            // Wait for user 
            Console.Read();
        }
    }

    // "Singleton" 

    class Singleton
    {
        private static Singleton instance;

        // Note: Constructor is 'protected' 
        protected Singleton()
        {
        }

        public static Singleton Instance()
        {
            // Use 'Lazy initialization' 
            if (instance == null)
            {
                instance = new Singleton();
            }

            return instance;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -