Why do we use the state template? To remove duplication of conditional logic and replace conditional code with polymorphism.
When do we have conditional duplication of logic? When we have many actions that depend on state, you should duplicate your conditional logic in each action. It becomes very annoying when you have many conditions. Also, code duplication means that you must update each copy of the duplicated code as you add new states.
So, if I don't have duplicate conditional logic, I would rather go with enum-based state instead of creating a new class hierarchy with many classes for states. Sometimes I even prefer duplicating conditional logic: for example. when I have many conditions, but only a few state-dependent actions. In this case, I prefer to have two switch blocks instead of creating ten new classes.
source share