Problem Statement
Model a domain with interfaces + abstract classes; justify composition vs inheritance.
Explanation
Use interfaces for contracts (what methods exist). Use abstract classes for shared implementation. Prefer composition to add features (e.g., Logging, Caching) without deep hierarchies. Inheritance fits when subclasses truly are specializations of one base behavior.
Code Solution
SolutionRead Only
interface Repository<T> { get(id: string): Promise<T>; save(x: T): Promise<void>; }
abstract class BaseRepo<T> implements Repository<T> {
constructor(protected store: Map<string, T>) {}
abstract validate(x: T): void;
async get(id: string){ return this.store.get(id)! }
async save(x: T){ this.validate(x); /*...*/ }
}
// Composition: wrap repo with a caching layer instead of subclass explosion