Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active January 23, 2026 09:38
Show Gist options
  • Select an option

  • Save sunmeat/c4f3ca97546f03eab89fd931142b1e5c to your computer and use it in GitHub Desktop.

Select an option

Save sunmeat/c4f3ca97546f03eab89fd931142b1e5c to your computer and use it in GitHub Desktop.
pattern decorator C# example
using System.Text;
namespace CoffeeShop
{
// розміри чашок у мілілітрах
internal enum Size { Small = 80, Medium = 180, Large = 280, ExtraLarge = 500 }
// базовий абстрактний напій
internal abstract class Beverage
{
protected Size size;
protected double price;
protected string? description;
public Size GetSize() => size;
public double GetPrice() => price;
public string? GetDescription() => description;
public void Print()
{
Console.WriteLine($"{description} = {price:F2} грн");
}
}
// чорний чай - конкретний базовий напій без добавок
internal class BlackTea : Beverage
{
public BlackTea(Size size)
{
this.size = size;
description = "чорний чай з пакетика";
price = size switch
{
Size.Medium => 35,
Size.Large => 45,
_ => throw new ArgumentException("неправильний розмір чашки")
};
}
}
// зелений чай - конкретний базовий напій без добавок
internal class GreenTea : Beverage
{
public GreenTea()
{
size = Size.Medium;
description = "зелений листовий чай";
price = 55;
}
}
// еспресо - конкретний базовий напій без добавок
internal class Espresso : Beverage
{
public Espresso()
{
size = Size.Small;
description = "мала порція міцної кави";
price = 45;
}
}
// базова абстрактна добавка
internal abstract class Condiment : Beverage
{
protected Beverage? beverage;
}
// молоко
internal class MilkCondiment : Condiment
{
public MilkCondiment(Beverage beverage)
{
this.beverage = beverage;
description = $"{this.beverage.GetDescription()} + молоко (5 грн)";
price = this.beverage.GetPrice() + 5;
size = this.beverage.GetSize();
}
public MilkCondiment() // молоко може бути основою напою
{
beverage = this;
description = "склянка молока";
price = 15;
size = Size.Medium;
}
}
// шоколад
internal class ChocolateCondiment : Condiment
{
public ChocolateCondiment(Beverage beverage)
{
this.beverage = beverage;
description = $"{this.beverage.GetDescription()} + шоколад (8 грн)";
price = this.beverage.GetPrice() + 8;
size = this.beverage.GetSize();
}
}
// цукор
internal class SugarCondiment : Condiment
{
public SugarCondiment(Beverage beverage)
{
this.beverage = beverage;
description = $"{this.beverage.GetDescription()} + цукор (2 грн)";
price = this.beverage.GetPrice() + 2;
size = this.beverage.GetSize();
}
}
internal static class Program
{
private static void Main()
{
Console.OutputEncoding = Encoding.UTF8;
var espresso = new Espresso();
var blackTea = new BlackTea(Size.Large);
var greenTea = new GreenTea();
espresso.Print();
blackTea.Print();
greenTea.Print();
Console.WriteLine("================================================");
var cappuccino = new SugarCondiment(new MilkCondiment(new Espresso()));
cappuccino.Print();
var chocolateCappuccino = new ChocolateCondiment(cappuccino);
chocolateCappuccino.Print();
var extraSweetBlackTea = new SugarCondiment(new SugarCondiment(new SugarCondiment(new BlackTea(Size.Large))));
extraSweetBlackTea.Print();
var glassOfMilk = new MilkCondiment();
glassOfMilk.Print();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment