How does DI relate to the Decorator Pattern?

Updated Apr 28, 2026

Short answer

DI makes implementing the Decorator Pattern seamless by allowing you to wrap implementations dynamically during registration.

Deep explanation

You can register a core implementation, and then register a decorator that implements the same interface but takes the core implementation in its constructor. This allows you to add cross-cutting concerns (like logging, caching, or retries) without modifying the original class.

Real-world example

Wrapping an HTTP client with a RetryPolicy client, and then wrapping that in a Logging client, all sharing the same interface.

Common mistakes

  • Modifying the original class to add caching logic instead of using a decorator, violating the Open-Closed Principle.

Follow-up questions

  • What is the Open-Closed Principle?

More Dependency Injection interview questions

View all →