Composition is good. But with dependency-injected subtyping, it’s better.
Object composition alone does not solve the problem of tight coupling.
If we compose with a concrete type, we’ve avoided inheritance but still hardcoded our dependency. We’ve traded one kind of rigidity for another rigidity.
What makes composition powerful is when the composed object is an abstraction — something that multiple concrete types can implement. And when that abstraction is passed in from the outside, rather than created from within, our code becomes more modular.
Instead of baking in behavior, we inject it. Instead of depending on a specific type, we depend on a shared contract.
Composition is truly powerful when we couple to abstractions and inject our dependencies.
📕 My book, The Object Oriented Way, explores this and other ways to write changeable software. Link in the comments.
Christopher Okhravi
Composition is good. But with dependency-injected subtyping, it’s better.
Object composition alone does not solve the problem of tight coupling.
If we compose with a concrete type, we’ve avoided inheritance but still hardcoded our dependency. We’ve traded one kind of rigidity for another rigidity.
What makes composition powerful is when the composed object is an abstraction — something that multiple concrete types can implement. And when that abstraction is passed in from the outside, rather than created from within, our code becomes more modular.
Instead of baking in behavior, we inject it. Instead of depending on a specific type, we depend on a shared contract.
Composition is truly powerful when we couple to abstractions and inject our dependencies.
📕 My book, The Object Oriented Way, explores this and other ways to write changeable software.
Link in the comments.
4 months ago | [YT] | 46