Dependency Injection
Dependency Injection
π¨βπΌ Perfect! You've demonstrated dependency injection in action.
π¦ Notice how
EmailService doesn't need to know about MockLogger or
SilentLogger. It just accepts any Logger (or subclass). This is the power
of composition:- Testability: Use
MockLoggerto verify behavior without side effects - Flexibility: Swap implementations at runtime
- Loose coupling:
EmailServicedoesn't depend on concrete logger classes
π° Dependency injection is a key benefit of composition. By accepting
dependencies through the constructor (or methods), you make your code more
flexible and testable. This is why composition is often preferred over
inheritanceβit gives you more control and flexibility.


