as a practice and selfdevelopment exercise I have decided to implement design patterns in C#. I am using polish cuisine as an example in my implementations. In this project I implemented Singleton design pattern. I kindly ask for a review :D Here is the code:
Model:
public sealed class Pierogi
{
public Guid Guid { get; }
public int Count { get; }
public string Type { get; }
public Pierogi(int count, string type)
{
Guid = Guid.NewGuid();
Count = count;
Type = type;
}
public override string ToString()
{
return $"Guid: {Guid}, Count: {Count}, Type: {Type}";
}
}
public static class PierogiTypes
{
public const string WithPotatoesAndCheese = "with potatoes and cheese";
public const string WithGroat = "with groat";
public const string WithCabbage = "with cabbage";
public const string WithMeat = "with meat";
public const string WithStrawberries = "with strawberries";
public const string WithBlueberries = "with blueberries";
public const string WithPotatoesAndCheeseButAlsoWithYoghurtAndKetchup = "with potatoes and cheese but also with yoghurt and ketchup";
}
Singleton:
public sealed class PierogiSingleton : IPierogi
{
private static readonly Lazy<PierogiSingleton> LazyInstance = new(() => new PierogiSingleton());
public static PierogiSingleton Instance => LazyInstance.Value;
public static bool IsInstanceCreated => LazyInstance.IsValueCreated;
public int PlatesServed = 0;
private PierogiSingleton()
{
}
public Pierogi ServePierogiWithPotatoesAndCheese()
{
PlatesServed += 1;
return new Pierogi(10, PierogiTypes.WithPotatoesAndCheese);
}
public Pierogi ServePierogiWithGroat()
{
PlatesServed += 1;
return new Pierogi(10, PierogiTypes.WithGroat);
}
public Pierogi ServePierogiWithCabbage()
{
PlatesServed += 1;
return new Pierogi(10, PierogiTypes.WithCabbage);
}
public Pierogi ServePierogiWithMeat()
{
PlatesServed += 1;
return new Pierogi(10, PierogiTypes.WithMeat);
}
public Pierogi ServePierogiWithStrawberries()
{
PlatesServed += 1;
return new Pierogi(10, PierogiTypes.WithStrawberries);
}
public Pierogi ServePierogiWithBlueberries()
{
PlatesServed += 1;
return new Pierogi(10, PierogiTypes.WithBlueberries);
}
public Pierogi ServePierogiWithPotatoesAndCheeseButAlsoWithYoghurtAndKetchup()
{
PlatesServed += 1;
return new Pierogi(10, PierogiTypes.WithPotatoesAndCheeseButAlsoWithYoghurtAndKetchup);
}
}
Program.cs:
class Program
{
public static async Task Work(int delay)
{
await Task.Delay(delay);
Console.WriteLine($"Is pierogi singleton instantiated: {PierogiSingleton.IsInstanceCreated}");
await Task.Delay(delay);
var firstPortionOfPierogi = PierogiSingleton.Instance.ServePierogiWithBlueberries();
Console.WriteLine($"{firstPortionOfPierogi}, Yummy :D");
Console.WriteLine($"Is pierogi singleton instantiated: {PierogiSingleton.IsInstanceCreated}");
await Task.Delay(delay);
var secondPortionOfPierogi = PierogiSingleton.Instance.ServePierogiWithPotatoesAndCheeseButAlsoWithYoghurtAndKetchup();
Console.WriteLine($"{secondPortionOfPierogi}, aaaah what is this sacrilege? :O");
Console.WriteLine($"Plates served {PierogiSingleton.Instance.PlatesServed}");
}
public static async Task Main()
{
Task.WaitAll(new Task[]
{
Task.Run(() => Work(500)),
Task.Run(() => Work(2500))
});
}
}