Strategy-like Patterns
There are a myriad of patterns out there which all basically remove the need for having complex hard coded if or switch statements, 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.

An overview on the specifics of each

Here is a little blurb on how some of the main varients of the strategy pattern differ, I will use the term handler a lot to indicate something that contains isolated logic.

Strategy Pattern

The strategy pattern is basically a way to dynamically pick a handler (some logic) based upon some predicate, the difference between this and other similar patterns is that in this case you would only apply one of the strategies that you have. So if you were to have 5 available IStateHandler implementations, only one should be applied.
i.e If you were a character who could only be in one state at a time, you could use this pattern to decide which state is applicable and run it

Chain of Responsibility

This pattern differs on a couple of fronts, generally you will be able to apply multiple handlers within a given scenario and there will be a single entry point which will delegate down to other handlers, this can be achieved by inheritance with recursive calls to same base methods to apply logic, or via a relay sort of system where you pass on the data to the next in the chain.
i.e If you were in a guild/clan and you had different roles which allowed access to different things, if you wanted to see if you had access to the guild bank you would need to go through each role handler to see if any of the ones applied to you allow you access to the bank.

State Pattern

This pattern is again very similar to the Strategy pattern in that only 1 handler will be active at once, but it generally operates back to front, so your handler (state) object takes the context (whatever your handling) and alters that depending on the current state then will potentially change the state on the context.
I would give you an example here but really I cannot think of a real use case for this, given there are so many similar patterns which are less invasive/coupled.

Scenario showing the control flow problem

So lets show a quick example of a problem where we could use one of these sort of patterns to improve the code.
1
public class Character
2
{
3
public EntityStates CurrentState { get; }
4
5
public void Update()
6
{
7
switch(CurrentState)
8
{
9
case EntityStates.Idle
10
{
11
// play an idle animation
12
// check for a timer to see if you need to do a random idle pose
13
// regen health
14
}
15
break;
16
17
case EntityStates.Patrolling
18
{
19
// move character towards waypoint
20
// play walking animation
21
// check for collisions
22
}
23
break;
24
25
case EntityStates.Fleeing
26
{
27
// move character in direction but faster
28
// play running animation
29
// check for collisions
30
// very similar to walking
31
}
32
break;
33
34
// More cases with more logic
35
}
36
}
37
}
Copied!
As you can see here we basically have a lot of states and the character can only be in one state at a given time. This works fine for the moment but as the game goes on we will keep adding more and more states, making it harder to maintain and potentially test as this Update method 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.

So lets make it better

So we want to make the switch statement more dynamic as well as not creating too much noise so the update is succinct and easy to test if needed, so lets look at a better approach.
1
public interface IStateHandler<T>
2
{
3
bool CanHandle(T data);
4
void Handle(T data);
5
}
Copied!
We can add an 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.
With this in place lets create some handlers to deal with the existing logic we would need in this scenario.
1
public class CharacterIdleStateHandler : IStateHandler<Character>
2
{
3
public bool CanHandle(Character character)
4
{ return character.CurrentState == EntityStates.Idle; }
5
6
public void Handle(Character character)
7
{
8
// play an idle animation
9
// check for a timer to see if you need to do a random idle pose
10
// regen health
11
}
12
}
13
14
public class CharacterPatrollingStateHandler : IStateHandler<Character>
15
{
16
public bool CanHandle(Character character)
17
{ return character.CurrentState == EntityStates.Patrolling; }
18
19
public void Handle(Character character)
20
{
21
// move character towards waypoint
22
// play walking animation
23
// check for collisions
24
}
25
}
26
27
public class CharacterFleeStateHandler : IStateHandler<Character>
28
{
29
public bool CanHandle(Character character)
30
{ return character.CurrentState == EntityStates.Fleeing; }
31
32
public void Handle(Character character)
33
{
34
// move character in a direction but faster
35
// play running animation
36
// check for collisions
37
// very similar to walking
38
}
39
}
Copied!
It is a bit more code, but we can now re-use our state handlers if needed and test them in isolation, we can also improve the character and make it more dynamic by just letting it take in all applicable state handlers via IoC then just loop through until we find the applicable one.
1
public class Character
2
{
3
public EntityStates CurrentState { get; }
4
5
// We can inject in any and all state handlers for the character
6
private IEnumerable<IStateHandler<Character>> characterStates;
7
8
public Character(IEnumerable<IStateHandler<Character>> states)
9
{ this.characterStates = states; }
10
11
public void Update()
12
{
13
// Loop through all our states finding the right one
14
foreach(var state in characterStates)
15
{
16
// Check if this state should handle it
17
if(state.CanHandle(this))
18
{
19
// Handle it and stop looping
20
state.Handle(this);
21
break;
22
}
23
}
24
}
25
}
Copied!
This whole bit is a lot to take in, but as you can see we have made it so you can easily add new handlers and the code for Character does 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 Character class. 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.

In the real world

In the real world we may want to stray slightly from the focus on 1 state handler and allow additive state handling, so you could potentially change the update method to:
1
public void Update()
2
{
3
// Loop through all our states finding the right one
4
foreach(var state in characterStates)
5
{
6
// Check if this state should handle it
7
if(state.CanHandle(this))
8
{ state.Handle(this); }
9
}
10
}
Copied!
This would allow you to have multiple handlers running per update, so for example if you wanted to have a character fleeing while attacking, or idling while healing, you could have multiple handlers that are simultaniously applicable, this opens up more doors to you while still keeping the benefits of the previous implementation. This is a half way house between Strategy and Chain of responsibility but as we are not internally deferring the logic we are not quite adhering to text book implementations of either pattern.
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 IStateHandler expose a state which it is tied to.

Final blurb

Like with any pattern, be pragmatic. They are generally guides on how to solve something, not rules. In this case there are soooooo many patterns which have a huge overlap with each other in this domain, ultimately the goal is to simplify code, increase flexibility/testability and isolate concerns, we can do that with any of these patterns but each of them offers a slightly different flavour, and while in some cases you may want to stick to one of them as a text book implementation, you don't want to limit yourself to just driving within the lines, go off road, see what wonders await you.
Last modified 2yr ago