Mid From PDF GoF Patterns Gang of Four Patterns

Concrete Decorators (MilkDecorator and SugarDecorator):?

Short answer: 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

Example code

{
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 }

Real-world example (ShopNest)

Logging and caching decorators wrap IProductRepository so core SQL code stays clean.

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
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