static void Main(string[] args) { Singleton singleton1 = Singleton.Instance; singleton1.PrintDetails("Message 1"); Singleton singleton2 = Singleton.Instance; singleton1.PrintDetails("Message 2"); Console.ReadLine(); }
private static void CreateStudent() { System.Threading.Thread.Sleep(2000); Singleton fromStudent = Singleton.GetInstance; fromStudent.PrintDetails("From Student"); }
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(); }
private static void PrintS1() { Singleton s1 = Singleton.GetInsance; s1.PrintDetails("This is first message"); }
private static void PrintS2() { Singleton s2 = Singleton.GetInsance; s2.PrintDetails("This is second message"); }
private static void GetSingletonInstance_2() { Singleton GetSingletonInstance_2 = Singleton.GetSingletonInstance; GetSingletonInstance_2.PrintDetails("PRINT : GetSingletonInstance_2"); }
private static void GetSingletonInstance_1() { Singleton GetSingletonInstance_1 = Singleton.GetSingletonInstance; GetSingletonInstance_1.PrintDetails("PRINT : GetSingletonInstance_1"); }
private static void PrintEmployeeDetails() { Singleton singletonEmp = Singleton.GetInstance; singletonEmp.PrintDetails("This is the first message for employees..."); }
private static void PrintContractorDetails() { Singleton singletonContract = Singleton.GetInstance; singletonContract.PrintDetails("Sending a message for Contractors ..."); }
private static void CreateEmployee() { Singleton fromEmployee = Singleton.GetInstance; fromEmployee.PrintDetails("From Employee"); }