When to Use Composition vs Inheritance

When to Use
πŸ‘¨β€πŸ’Ό Let's build a notification system. We need to decide whether to use inheritance or composition for different scenarios.
🐨 Open
index.ts
and:
  1. Create a Logger class with a log(message: string) method
  2. Create an EmailService class that uses composition (has-a Logger)
  3. Create a FileLogger class that uses inheritance (is-a Logger)
  4. Create a ConsoleLogger class that uses inheritance (is-a Logger)
  5. Demonstrate when each approach is appropriate
πŸ’° Think about the relationship: Is it "is-a" or "has-a"?

Please set the playground first

Login to get access to the exclusive discord channel.
Loading Discord Posts