Mid From PDF GoF Patterns Gang of Four Patterns

Decorator (CoffeeDecorator): ○ The CoffeeDecorator class is an abstract class that implements the ICoffee interface and holds a reference to an ICoffee object. This class

Short answer: llows us to add additional behavior to the coffee object, but we don't modify the base class (SimpleCoffee).

Explain a bit more

public abstract class CoffeeDecorator : ICoffee { protected ICoffee _coffee; protected CoffeeDecorator(ICoffee coffee) => _coffee = coffee; public virtual double Cost() => _coffee.Cost(); public virtual string Description() => _coffee.Description(); } llows us to add additional behavior to the coffee object, but we don't modify the base class (SimpleCoffee). public abstract class CoffeeDecorator : ICoffee { protected ICoffee _coffee; protected CoffeeDecorator(ICoffee coffee) => _coffee = coffee; public virtual double Cost() => _coffee.Cost(); public virtual string Description() => _coffee.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