Delegation solution
Delegation is often considered an alternative to the polymorphic solution. In this approach, an extension of a function is implemented by delegating part of the responsibility to other classes and then extending its behaviors. As a result, there is no mandatory requirement to create a subclass. There are several reasons for that:
- Loose coupling and high cohesion
- Separation of concerns
- Easy substitution
- Refactoring to the delegation solution
Loose coupling and high cohesion
Using delegation, code can be reused and composed only for the parts that are needed. It is more flexible than inheriting a class that is likely to give the subclasses more than it needs. This results in looser coupling to the reused code while still maintaining high cohesion within the class.
Separation of concerns
With the use of delegation, classes can be broken into small classes (sub-classes) that only have a single responsibility. These classes are only...