State
AKA: objects for states
At some point, the logic in an object might become cumbersome, branching on
attributes implying state. As those implicit states become essential to how the
program runs across more and more methods, the chance for mistakes increases.
Instead of using these implicit states, we migrate to an enum
. But then we
have introduced a switch into our code. These are often a sign of a
non-extendable design.
Instead, we want to continue growing the program to completion but are aware of the risks of these state variables. It would be better to encapsulate the state in some way where it did not need to use a switch but still could transition as before.
To do this, our Stateful object changes its implicit class by holding onto an
instance with the desired behaviour, a
The
I avoid the presented implementation of this pattern as I find it more
complicated than helpful. Instead, I prefer when a
“I am, therefore I think.”