示例#1
0
        static void Main(string[] args)
        {
            Singleton singleton1 = Singleton.Instance;

            singleton1.PrintDetails("Message 1");
            Singleton singleton2 = Singleton.Instance;

            singleton1.PrintDetails("Message 2");
            Console.ReadLine();
        }
示例#2
0
        private static void CreateStudent()
        {
            System.Threading.Thread.Sleep(2000);
            Singleton fromStudent = Singleton.GetInstance;

            fromStudent.PrintDetails("From Student");
        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter one of the following number to explore the design pattern");
            Console.WriteLine("1- Singleton Pattern");
            Console.WriteLine("2- Singleton Thread Safety with lock Pattern");
            string value = Console.ReadLine();

            switch (value)
            {
            case "1":
                Singleton objectA = Singleton.GetInstance;
                objectA.PrintDetails("This is from Object A");
                Singleton objectB = Singleton.GetInstance;
                objectA.PrintDetails("This is from Object B");
                //Uncomment following code if we want to test why we need to seal our class.
                //Singleton.DerivedSingleton derivedObj = new Singleton.DerivedSingleton();
                //derivedObj.PrintDetails("From Derived");
                break;

            case "2":
                Parallel.Invoke(
                    () => SingletonHelper.PrintObjectA1Details(),
                    () => SingletonHelper.PrintObjectB1Details()
                    );
                break;

            case "3":
                SingletonEager objectAE = SingletonEager.GetInstance;
                objectAE.PrintDetails("This is from eager loading, obeject AE");
                SingletonEager objectBE = SingletonEager.GetInstance;
                objectAE.PrintDetails("This is from eager loading, obeject BE");
                break;

            case "D":
                break;
            }
            Console.Read();
        }
示例#4
0
        private static void PrintS1()
        {
            Singleton s1 = Singleton.GetInsance;

            s1.PrintDetails("This is first message");
        }
示例#5
0
        private static void PrintS2()
        {
            Singleton s2 = Singleton.GetInsance;

            s2.PrintDetails("This is second message");
        }
示例#6
0
        private static void GetSingletonInstance_2()
        {
            Singleton GetSingletonInstance_2 = Singleton.GetSingletonInstance;

            GetSingletonInstance_2.PrintDetails("PRINT : GetSingletonInstance_2");
        }
示例#7
0
        private static void GetSingletonInstance_1()
        {
            Singleton GetSingletonInstance_1 = Singleton.GetSingletonInstance;

            GetSingletonInstance_1.PrintDetails("PRINT : GetSingletonInstance_1");
        }
示例#8
0
        private static void PrintEmployeeDetails()
        {
            Singleton singletonEmp = Singleton.GetInstance;

            singletonEmp.PrintDetails("This is the first message for employees...");
        }
示例#9
0
        private static void PrintContractorDetails()
        {
            Singleton singletonContract = Singleton.GetInstance;

            singletonContract.PrintDetails("Sending a message for Contractors ...");
        }
示例#10
0
        private static void CreateEmployee()
        {
            Singleton fromEmployee = Singleton.GetInstance;

            fromEmployee.PrintDetails("From Employee");
        }