PatternsCSharpProgramming/Patterns/Singleton/Program.cs

52 lines
1.2 KiB
C#
Raw Normal View History

2025-05-12 03:32:04 +03:00
/*
* Author: Nikolay Dvurechensky
* Site: https://www.dvurechensky.pro/
* Gmail: dvurechenskysoft@gmail.com
* Last Updated: 12 мая 2025 03:31:02
* Version: 1.0.7
*/
/* Одиночка
2024-10-05 09:15:54 +03:00
Гарантирует что класс имеет только
один экземпляр и представляет глобальную
точку доступа к нему
*/
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"};
}
}