State pattern is pretty nice solution for solving problems related to changing behavior of some component(class) at runtime, which varies depending on current state of that object. Clients of our class having dynamic behavior have an impression that, upon interaction with that class, it seems like there’s different implementation of that object as of that interaction. There’s no magic involved – we’re just using composition, and our dynamic object just delegates call to
State object it encapsulates. Interface of
State object defines set of actions (methods) that change behavior of our wrapping class (set of actions that cause system to transition).
State implementations are those that are in charge of making transitions from state to state.
If you feel curious how would an implementation of decorator design pattern look like in Kotlin, this might be the right place for you. This example is just a very basic thing that you then tweak until it’s perfect. Pattern definition can be found at Wiki, but what’s important is that you can compose chain of decorators at runtime and in such a way – you can control runtime behavior of your system.