Sealed class solution
The Kotlin language has a feature called sealed class, which restricts the class hierarchy and requires that all subclasses be defined at compile time. All subclasses need to be in the same package and module where the sealed class is defined. This also means that no third-party classes can be inherited from sealed classes.
Here are a few observations from the polymorphic solution mentioned previously. Firstly, all subclasses have a startedAt
field and an implementation of the performService
function that sets the startedAt
field. So, the sealed class solution can be modified from the polymorphic solution. The interface can be changed to a sealed class with the startedAt
field and the performService
function:
sealed class Service { var startedAt: Instant? = null fun performService(time: Instant) { startedAt = time } &...