Comparing all the solutions
All these solutions are valid, though their styles vary a lot. It is important to understand the pros and cons of each approach so we can make an informed decision to apply the solution wherever appropriate:
- Extensibility:
- Polymorphic: Extensible outside package and module
- Sealed classes: Not extensible outside package or module
- Delegation: Extensible outside package and module
- Functional: Extensible outside package and module
- Readability and code cleanness:
- Polymorphic: Subclasses may inherit unnecessary features from superclasses, creating noise while reading code; classes can be big
- Sealed classes: All subclasses are known at compile time; no missing branches; not suitable for too many subclasses
- Delegation: Small interfaces; multiple behavioral delegations can be complicated; promotes single responsibility per interface; only delegates behaviors on demand
- Functional: Small classes and functions; easy to reason about immutable data and pure functions...