PatternsCSharpExtraAddons/Patterns/Pattern_1-Синглтон(Singleton)/Program.cs
Dvurechensky a4cd4b4ced 1.0
Main
2024-10-05 09:30:14 +03:00

33 lines
871 B
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.

/*
* ПОРОЖДАЮЩИЕ ПАТТЕРНЫ
*
* Глава_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);
}
}