PatternsCSharpProgramming/Patterns/Singleton/Program.cs
Dvurechensky 3a28caed27 1.0
Main
2024-10-05 09:15:54 +03:00

44 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Одиночка
Гарантирует что класс имеет только
один экземпляр и представляет глобальную
точку доступа к нему
*/
class Program
{
static void Main()
{
#region Пример 1 - базовое
(new Thread(() =>
{
Console.WriteLine(GameHistory.Instance.History[1]);
})).Start();
Console.WriteLine(GameHistory.Instance.History[0]);
Console.ReadKey();
#endregion
}
}
class GameHistory
{
private static object syncRoot = new();
private static GameHistory _instance;
public static GameHistory Instance
{
get
{
lock(syncRoot)
{
if(_instance == null)
_instance = new GameHistory();
}
return _instance;
}
}
public string[] History { get; set; }
private GameHistory()
{
History = new[] { "One History",
"Two History"};
}
}