Advertisement
yakovmonarh

Одиночка (Singleton)

Aug 20th, 2019
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.    static void Main(string[] args)
  6.    {
  7.         Computer comp = new Computer();
  8.         comp.Launch("WinXP");
  9.         Console.WriteLine( comp.OS.Name);
  10.        
  11.         comp.OS = OS.GetInstance("win7");
  12.         Console.WriteLine(comp.OS.Name);
  13.        
  14.         Console.ReadLine();
  15.    }
  16. }
  17.  
  18. class Computer
  19. {
  20.     public OS OS {get;set;}
  21.    
  22.     public void Launch(string osName)
  23.     {
  24.         this.OS = OS.GetInstance(osName);
  25.     }
  26. }
  27.  
  28. class OS
  29. {
  30.     private static OS instance;
  31.    
  32.     public string Name{get; private set;}
  33.    
  34.     protected OS(string name)
  35.     {
  36.         this.Name = name;
  37.     }
  38.    
  39.     public static OS GetInstance(string name)
  40.     {
  41.         if(instance == null)
  42.         {
  43.             instance = new OS(name);
  44.         }
  45.         return instance;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement