Mid From PDF GoF Patterns Gang of Four Patterns

Concrete Decorators (MilkDecorator and SugarDecorator):?

  • The MilkDecorator and SugarDecorator classes extend the

CoffeeDecorator class and override the Cost() and Description()

methods to add specific behavior (like adding the price of milk or sugar).

MilkDecorator:

public class MilkDecorator : CoffeeDecorator
{
public MilkDecorator(ICoffee coffee) : base(coffee) { }
public override double Cost() => base.Cost() + 0.50; // Add cost

of milk

public override string Description() => base.Description() + ",

Milk"; // Add milk to description

}

SugarDecorator:

public class SugarDecorator : CoffeeDecorator
{
public SugarDecorator(ICoffee coffee) : base(coffee) { }
public override double Cost() => base.Cost() + 0.25; // Add cost

of sugar

public override string Description() => base.Description() + ",

Sugar"; // Add sugar to description

}

More from Design Patterns in C#

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details