As a practice and self-development 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 Prototype design pattern.
Model:
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";
}
Desgin Pattern:
using Creational.Prototype.Model;
namespace Creational.Prototype.DesignPatters;
public abstract class PierogiPrototype
{
public Guid Guid { get; }
public int Count { set; get; }
public string Type { get; set; }
public PierogiPrototype(int count)
{
Guid = Guid.NewGuid();
Count = count;
}
public abstract PierogiPrototype Clone();
public override string ToString()
{
return $"Guid: {Guid}, Count: {Count}, Type: {Type}";
}
}
public class PierogiWithPotatoesAndCheese : PierogiPrototype
{
public PierogiWithPotatoesAndCheese(int count) : base(count)
{
Type = PierogiTypes.WithPotatoesAndCheese;
}
public override PierogiPrototype Clone()
{
return (PierogiPrototype)MemberwiseClone();
}
}
.
.
.
.
.
public class PierogiWithBlueberries : PierogiPrototype
{
public PierogiWithBlueberries(int count) : base(count)
{
Type = PierogiTypes.WithBlueberries;
}
public override PierogiPrototype Clone()
{
return (PierogiPrototype)MemberwiseClone();
}
}
Program:
class Program
{
public static void Main()
{
var firstPortion = new PierogiWithBlueberries(10);
Console.WriteLine(firstPortion);
var secondPortion = firstPortion.Clone();
Console.WriteLine(secondPortion);
firstPortion.Count = 5;
Console.WriteLine(firstPortion);
Console.WriteLine(secondPortion);
}
}