Caminhoneiro

Singleton

Oct 2nd, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. //1 Add to the class a private static field that will hold the singleton instance.
  8.  
  9. //2 Declare public static creation method that will be used for retrieving singleton instance.
  10.  
  11. //3 Implement "lazy initialization" inside the creation method.It should create a new instance on the first call and put it into the static field.
  12.   //The method should return that instance in all subsequent calls.
  13.  
  14. //4 Make class constructor private.
  15.  
  16. //5 In client code replace all direct constructor calls with calls to the static creation method.
  17.  
  18.  
  19. namespace Singleton
  20. {
  21.    
  22.     //1 Add to the class a private static field that will hold the singleton instance.
  23.     class Singleton
  24.     {
  25.         //2 Declare public static creation method that will be used for retrieving singleton instance.
  26.         private static Singleton instance;
  27.  
  28.  
  29.         //3 Implement "lazy initialization" inside the creation method.It should create a new instance on the first call and put it into the static field.
  30.         //The method should return that instance in all subsequent calls.
  31.         private static object obj = new object();
  32.  
  33.         //4 Make class constructor private.
  34.         private Singleton() { }
  35.  
  36.         public static Singleton GetInstace()
  37.         {
  38.             lock (obj)
  39.             {
  40.                 if (instance == null)
  41.                     instance = new Singleton();
  42.             }
  43.  
  44.             return instance;
  45.         }
  46.  
  47.     }
  48.  
  49.     class Client
  50.     {
  51.         //5 In client code replace all direct constructor calls with calls to the static creation method.
  52.         public void ClientCode()
  53.         {
  54.             Singleton s1 = Singleton.GetInstace();
  55.             Singleton s2 = Singleton.GetInstace();
  56.             Singleton s3 = Singleton.GetInstace();
  57.             Singleton s4 = Singleton.GetInstace();
  58.  
  59.  
  60.             if (s1 == s2 && s3 == s4)
  61.             {
  62.                 Console.WriteLine("Singleton works, both variables contain the same instance.");
  63.             }
  64.             else
  65.             {
  66.                 Console.WriteLine("Singleton failed, variables contain different instances.");
  67.             }
  68.  
  69.         }
  70.     }
  71.  
  72.     class Program
  73.     {
  74.         static void Main(string[] args)
  75.         {
  76.             Console.WriteLine("Singleton start");
  77.  
  78.             Client client = new Client();
  79.             client.ClientCode();
  80.             Console.ReadKey();
  81.            
  82.         }
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment