PatternsCSharpExtraAddons/Patterns/Pattern_1-Синглтон(Singleton)/Program.cs
Dvurechensky 5803b59f38 1.0.2
2025-05-12 03:44:31 +03:00

41 lines
1.0 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.

/*
* Author: Nikolay Dvurechensky
* Site: https://www.dvurechensky.pro/
* Gmail: dvurechenskysoft@gmail.com
* Last Updated: 12 мая 2025 03:39:52
* Version: 1.0.4
*/
/*
* ПОРОЖДАЮЩИЕ ПАТТЕРНЫ
*
* Глава_8: Сиглтон, Одиночка (Singleton)
*
* - гантирует, что у класса есть только один экземпляр,
* и предоставляет глобальную точку доступа к нему
*/
class Singleton
{
private Singleton()
{
Data = 28;
MoreData = 90;
}
public int Data { get; set; }
public int MoreData { get; set; }
/// <summary>
/// Отложенный синглтон (ленивый одиночка)
/// </summary>
static Lazy<Singleton> uniqueInstance = new Lazy<Singleton>(() => new Singleton());
public static Singleton Instance => uniqueInstance.Value;
}
class Program
{
public static void Main()
{
Console.WriteLine(Singleton.Instance.Data);
}
}