Dependency Injection
Dependency Injection
π¨βπΌ Now let's see the power of composition! We can swap different logger
implementations without changing
EmailService. This is dependency injectionβa
key benefit of composition.π¨ Open
and:
- Create a
MockLoggerclass that extendsLoggerand stores logs in memory (useful for testing) - Create a
SilentLoggerclass that extendsLoggerand does nothing (useful for production) - Both should extend the
Loggerclass from step 01 - Demonstrate that
EmailServiceworks with any logger implementation
π° This is the power of dependency injection:
EmailService doesn't care which
logger it getsβit just uses whatever logger is provided. This makes testing easy
and allows you to swap implementations at runtime.π Dependency Injection


