コード例 #1
0
        /// <summary>
        /// Tests the Singleton Pattern
        /// </summary>
        public static void TestSingleton()
        {
            Singleton singleton = Singleton.getInstance();

            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Get new Instance");
            singleton = Singleton.getInstance();
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Dispose");
            Singleton.Dispose();
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Get new Instance after disposal");
            singleton = Singleton.getInstance();
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Get BigSingleton (old object should not be replaced)");
            singleton = BigSingleton.getInstance();
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Get BigSingleton after disposal");
            Singleton.Dispose();
            singleton = BigSingleton.getInstance();
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
            Console.WriteLine("Singleton Data: {0}", singleton.Data);
        }
コード例 #2
0
        public static Singleton getInstance()
        {
            if (instance == null)
            {
                instance = new BigSingleton();
            }

            return(instance);
        }