|
| 1 | +// EN: Singleton Design Pattern |
| 2 | +// |
| 3 | +// Intent: Lets you ensure that a class has only one instance, while providing a |
| 4 | +// global access point to this instance. |
| 5 | +// |
| 6 | +// RU: Паттерн Одиночка |
| 7 | +// |
| 8 | +// Назначение: Гарантирует, что у класса есть только один экземпляр, и |
| 9 | +// предоставляет к нему глобальную точку доступа. |
| 10 | + |
| 11 | +using System; |
| 12 | +using System.Threading; |
| 13 | + |
| 14 | +namespace Singleton |
| 15 | +{ |
| 16 | + // EN: The Singleton class defines the `getInstance` method that lets |
| 17 | + // clients access the unique singleton instance. |
| 18 | + // |
| 19 | + // RU: Класс Одиночка предоставляет метод getInstance, который позволяет |
| 20 | + // клиентам получить доступ к уникальному экземпляру одиночки. |
| 21 | + class Singleton |
| 22 | + { |
| 23 | + private static Singleton _instance; |
| 24 | + |
| 25 | + private static readonly object _lock = new object(); |
| 26 | + |
| 27 | + private string _value; |
| 28 | + |
| 29 | + public Singleton(string value) |
| 30 | + { |
| 31 | + this._value = value; |
| 32 | + } |
| 33 | + |
| 34 | + public string GetValue() |
| 35 | + { |
| 36 | + return this._value; |
| 37 | + } |
| 38 | + |
| 39 | + // EN: The static method that controls the access to the singleton |
| 40 | + // instance. |
| 41 | + // |
| 42 | + // This implementation let you subclass the Singleton class while |
| 43 | + // keeping just one instance of each subclass around. |
| 44 | + // |
| 45 | + // RU: Статический метод, управляющий доступом к экземпляру одиночки. |
| 46 | + // |
| 47 | + // Эта реализация позволяет вам расширять класс Одиночки, сохраняя |
| 48 | + // повсюду только один экземпляр каждого подкласса. |
| 49 | + public static Singleton GetInstance(string value) |
| 50 | + { |
| 51 | + lock (_lock) |
| 52 | + { |
| 53 | + if (_instance == null) |
| 54 | + { |
| 55 | + _instance = new Singleton(value); |
| 56 | + } |
| 57 | + } |
| 58 | + return _instance; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + class Program |
| 63 | + { |
| 64 | + static void Main(string[] args) |
| 65 | + { |
| 66 | + // EN: The client code. |
| 67 | + // |
| 68 | + // RU: Клиентский код. |
| 69 | + |
| 70 | + Console.WriteLine( |
| 71 | + "{0}\n{1}\n\n{2}\n", |
| 72 | + "If you see the same value, then singleton was reused (yay!)", |
| 73 | + "If you see different values, then 2 singletons were created (booo!!)", |
| 74 | + "RESULT:" |
| 75 | + ); |
| 76 | + |
| 77 | + Thread process1 = new Thread(() => { |
| 78 | + TestSingleton("FOO"); |
| 79 | + }); |
| 80 | + Thread process2 = new Thread(() => { |
| 81 | + TestSingleton("BAR"); |
| 82 | + }); |
| 83 | + process1.Start(); |
| 84 | + process2.Start(); |
| 85 | + } |
| 86 | + |
| 87 | + static public void TestSingleton(string value) |
| 88 | + { |
| 89 | + Singleton singleton = Singleton.GetInstance(value); |
| 90 | + Console.WriteLine(singleton.GetValue()); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments