singletonstructural.cs

来自「使用C#程序将23个常用设计模式进行列表显示」· CS 代码 · 共 46 行

CS
46
字号
using System;
using System.Collections;
using System.Windows.Forms;

namespace DesignPattern.SingletonStructural
{
    class SingletonStructural : AbstractPattern
    {
        public static void Run(TextBox tbInfo)
        {
            s_tbInfo = tbInfo;
            s_tbInfo.Text = "";

            //base.Run(tbInfo);
            // Constructor is protected -- cannot use new 
            Singleton s1 = Singleton.Instance();
            Singleton s2 = Singleton.Instance(); 
            if (s1 == s2) 
            {
                DesignPattern.FormMain.OutputInfo("Objects are the same instance");
                //DesignPattern.FormMain.OutputInfo("Objects are the same instance"); 
            } 
            // Wait for user 
            ////Console.Read();
        }
    }

    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 + =
减小字号Ctrl + -
显示快捷键?