예제 #1
0
        public static TSSingleton Instance()
        {
            // Uses locking.

            // Note: this is  thread safe.
            lock (singletonLock)
            {
                if (_instance == null)
                {
                    _instance = new TSSingleton();
                }
            }
            return(_instance);
        }
예제 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Singleton_Structural_Console");

            // Constructor is protected -- cannot use new

            TSSingleton s1 = TSSingleton.Instance();
            TSSingleton s2 = TSSingleton.Instance();

            // Test for same instance

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

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }