Summary
We used the example of three types of services (plumbing, babysitting, and room cleaning) that households can perform for each other, focusing on the start, completion, confirmation, and checks for whether the service was performed.
We presented a solution that uses traditional polymorphism in object-oriented programming. An interface was defined and implemented by three subclasses, one for each type of service. The main
function uses these subclasses in a homogeneous yet polymorphic manner.
We then used the Kotlin sealed class feature to restrict all subclasses to be known. A further variation was that the sealed class was used together with the when
construct to handle all branches within the when
block. It resulted in a function containing all variations of the service check behaviors suitable for a small and fixed number of subclasses in a package.
We presented an alternative solution that uses Kotlin delegation over polymorphism. We defined smaller interfaces...