Example #1
0
 static void Main(string[] args)
 {
     for (int i = 0; i < 100; i++)
     {
         LazySingleton s = LazySingleton.Instance;
         s.PrintName();
     }
 }
Example #2
0
 public LazySingleton GetInstance()
 {
     if (Instance == null)
     {
         Instance = new LazySingleton();
     }
     return(Instance);
 }
Example #3
0
 public static LazySingleton GetInstance()
 {
     if (s == null)
     {
         s = new LazySingleton();
     }
     return(s);
 }
Example #4
0
        public static LazySingleton getInstance()
        {
            if (instanceSingleton == null)
            {
                instanceSingleton = new LazySingleton();
            }

            return(instanceSingleton);
        }
 public static LazySingleton getInstance()
 {
     // 每个线程来之前先等待锁
     lock (syncObject)
     {
         if (instance == null)
         {
             instance = new LazySingleton();
         }
     }
     return(instance);
 }
Example #6
0
        static void Main(string[] args)
        {
            //Singleton s1 = Singleton.GetInstance();
            //Singleton s2 = Singleton.GetInstance();
            //Console.WriteLine(s1 == s2);
            Singleton.Test();
            //Console.WriteLine(Singleton.VERSION);

            //LazySingleton ls1 = LazySingleton.GetInstance();
            //LazySingleton ls2 = LazySingleton.GetInstance();
            LazySingleton.Test();
        }
        static void Main(string[] args)
        {
            LazySingleton ls1 = LazySingleton.GetInstance();

            ls1.Show();
            LazySingleton ls2 = LazySingleton.GetInstance();

            ls2.Show();
            LazySingleton ls3 = LazySingleton.GetInstance();

            ls3.Show();

            Console.ReadLine();
        }
Example #8
0
        static void Main(string[] args)
        {
            Console.WriteLine("************************************");
            Console.WriteLine("Eager EagerSingleton Test");
            Console.WriteLine("************************************");

            //These Should Pass
            EagerSingleton eagerSingleton1 = EagerSingleton.Instance;
            EagerSingleton eagerSingleton2 = EagerSingleton.Instance;

            Console.WriteLine("Is eager1==eager2: " + (eagerSingleton1 == eagerSingleton2));
            Console.WriteLine("Extra test for fun: " + Object.ReferenceEquals(eagerSingleton1, eagerSingleton2));
            Console.WriteLine();

            Console.WriteLine("************************************");
            Console.WriteLine("Lazy EagerSingleton Test");
            Console.WriteLine("************************************");

            //These Should Pass
            LazySingleton lazySingleton1 = LazySingleton.Instance;
            LazySingleton lazySingleton2 = LazySingleton.Instance;

            Console.WriteLine("Is lazy1==lazy2: " + (lazySingleton1 == lazySingleton2));
            Console.WriteLine("Extra test for fun: " + Object.ReferenceEquals(lazySingleton1, lazySingleton2));
            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine("************************************");
            Console.WriteLine("Generic Lazy Loading EagerSingleton Test");
            Console.WriteLine("************************************");

            //These Should Pass
            Test1 test1A = Singleton <Test1> .Instance;
            Test1 test1B = Singleton <Test1> .Instance;

            Console.WriteLine("Is t1A==t1B: " + (test1A == test1B));
            Console.WriteLine("Extra test for fun: " + Object.ReferenceEquals(test1A, test1B));
            Console.WriteLine();

            //These should Fail
            Test2 test2A = Singleton <Test2> .Instance;
            Test2 test2B = new Test2();

            Console.WriteLine("Is t2A==t2B: " + (test2A == test2B));
            Console.WriteLine("Extra test for fun: " + Object.ReferenceEquals(test2A, test2B));
            Console.WriteLine();

            Console.ReadKey();
        }
Example #9
0
 public LazySingleton GetInstance()
 {
     if (singleton == null)
     {
         //唯一线程访问
         lock (_lock)
         {
             if (singleton == null)
             {
                 return(singleton = new LazySingleton());
             }
         }
     }
     return(singleton);
 }
Example #10
0
 public static void Test()
 {
     Console.WriteLine($"Name: {LazySingleton.GetInstance().Name}");
 }
Example #11
0
        static void Main()
        {
            /*
             * Structural
             */

            // constructors are protected, cannot use "new"
            StructuralSingleton structuralSingletonOne = StructuralSingleton.Instance();
            StructuralSingleton structuralSingletonTwo = StructuralSingleton.Instance();

            // test for instance equality
            if (structuralSingletonOne == structuralSingletonTwo)
            {
                Console.WriteLine("Objects are same instance");
            }

            /*
             * Real-World
             */

            RealWorldSingletonLoadBalancer balancerOne   = RealWorldSingletonLoadBalancer.GetLoadBalancer();
            RealWorldSingletonLoadBalancer balancerTwo   = RealWorldSingletonLoadBalancer.GetLoadBalancer();
            RealWorldSingletonLoadBalancer balancerThree = RealWorldSingletonLoadBalancer.GetLoadBalancer();
            RealWorldSingletonLoadBalancer balancerFour  = RealWorldSingletonLoadBalancer.GetLoadBalancer();

            // test for equality
            if (balancerOne == balancerTwo && balancerTwo == balancerThree && balancerThree == balancerFour)
            {
                Console.WriteLine("Same instance");
            }

            // load balance 15 server requests
            RealWorldSingletonLoadBalancer loadBalancer = RealWorldSingletonLoadBalancer.GetLoadBalancer();

            for (int i = 0; i < 15; i++)
            {
                string server = loadBalancer.Server;
                Console.WriteLine("Dispatch request to: " + server);
            }

            /*
             * .NET Optimized
             */

            DotNetOptimizedSingleton dotNetBalancerOne   = DotNetOptimizedSingleton.GetLoadBalancer();
            DotNetOptimizedSingleton dotNetBalancerTwo   = DotNetOptimizedSingleton.GetLoadBalancer();
            DotNetOptimizedSingleton dotNetBalancerThree = DotNetOptimizedSingleton.GetLoadBalancer();
            DotNetOptimizedSingleton dotNetBalancerFour  = DotNetOptimizedSingleton.GetLoadBalancer();

            // test for equality
            if (dotNetBalancerOne == dotNetBalancerTwo && dotNetBalancerTwo ==
                dotNetBalancerThree && dotNetBalancerThree == dotNetBalancerFour)
            {
                Console.WriteLine("Same instance");
            }

            // load balance 15 server requests
            DotNetOptimizedSingleton dotNetBalancer = DotNetOptimizedSingleton.GetLoadBalancer();

            for (int i = 0; i < 15; i++)
            {
                string server = dotNetBalancer.NextServer.Name;
                Console.WriteLine("Dispatch request to: " + server);
            }

            /*
             * Lazy
             */

            LazySingleton lazySingletonOne = LazySingleton.Instance;
            LazySingleton lazySingletonTwo = LazySingleton.Instance;

            if (lazySingletonOne == lazySingletonTwo)
            {
                Console.WriteLine("Lazy Singletons are equal");
            }

            Console.WriteLine(lazySingletonOne.CheckCreation());
        }
Example #12
0
        private static void PrintStudentdetails()
        {
            LazySingleton fromStudent = LazySingleton.GetInstance;

            fromStudent.PrintDetails("From Student");
        }
Example #13
0
        private static void PrintTeacherDetails()
        {
            LazySingleton fromTeacher = LazySingleton.GetInstance;

            fromTeacher.PrintDetails("From Teacher");
        }
 static Nested()
 {
     instance = new LazySingleton();
 }