switchstatements, they all basically do the same thing, which is remove the need for the hardcoded control flow and replace it with something more dynamic, be it a single class that you call which does it all for you, or a set of objects you loop through. Rather than detail each one individually, I will just group them all together under the same banner and give you a bit of info on what the differences are between some of the main patterns.
Patterns are just guides as to how to solve a problem, you can stick to a pattern exactly how the text books or just see the high level benefit and see how it effects your scenario, in this example I am going to make use of a bit of each pattern while not really sticking to any individual one just to show that it can make sense to be a bit pragmatic with patterns at times.
IStateHandlerimplementations, only one should be applied.
Updatemethod will end up just becoming a dumping ground for logic.
You may have thought "we could use composition to reduce the noise in the cases" which you could do and that would be a step in the right direction, but you would still need to explicitly have an instance of each state in the character, which would in turn grow as the concerns the character can handle increases.
IStateHandler<T>which lets us define an object which will check if it can be run, and encapsulate the logic needed to run if it is a match.
Characterdoes not need to change, this is a huge win as we can now test our handlers in isolation, and if they need dependencies to operate we can inject them in without polluting the
Characterclass. This is adhering primarily to the Strategy pattern but we could easily alter the scenario and it starts blurring into some amalgamation of the other patterns.
If we were going to stick on this approach where there was only a single handler you could optimize further by having each state handler bound to a specific state in a dictionary so you dont need to keep looping every update, this would probably be better implied by making each
IStateHandlerexpose a state which it is tied to.