Mid
From PDF
GoF Patterns
Gang of Four Patterns
Decorator (CoffeeDecorator):?
Short answer: The CoffeeDecorator class is an abstract class that implements the ICoffee interface and holds a reference to an ICoffee object. This class allows us to add additional behavior to the coffee object, but we don't modify the base class (SimpleCoffee). public abstract class CoffeeDecorator : ICoffee
Example code
{ 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
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png