Strategy Design Pattern, Swiftly Explained With The Three Kingdoms
While reading through common programming design patterns and learning them thoroughly, I feel that strategy design pattern is the most intriguing. Mostly because of the cool name and also its practicality in mobile development. Implementing the pattern can decouple algorithm from a class that’s using it. This boosts reusability in the code and make mr. “SOLID principle” proud.
The pattern is quite simple in its application, but to simplify it even more I’ll explain with the narrative of Three Kingdoms which I think is quite suitable when talking about strategies and will make some reader more interested in reading this. Yes, I’m looking at you, Dynasty Warriors players!
So here goes…
Once upon a time there are divisions of power in the land of ancient China. Three kingdoms hold the most power, namely Shu, Wei and Wu. Attacking each other has become everyday’s breakfast and supper, mainly to claim territory for their own.
Wei decided to attack Shu using their usual tactics. Sima Yi, being their usual tactician gives the attack plan to the Wei army. He is feared for his cunning tactic in the battlefield which leaves the enemy of Wei trembling in fear and often gives Wei its victory on a silver plating.
Everything goes well for the Wei army that day. But hold everyone’s horses! let’s say hypothetically Sima Yi suddenly get some critical illness after the attack, wouldn’t this make Wei’s next attack get a null reference…uh, I mean, wouldn’t this make Wei can’t attack because they lose their treasured tactician?
This is obviously not looking good for Wei. This won’t happen if Wei have a better structure in their ranks so that when some high ranking strategist dies, they just mourn for a little while and then able to resume their kingdomly thingy like usual business. There’s gotta be a better way!
Meanwhile let’s see the opposite faction, Shu.
Shu is famous with their trio: Liu Bei, Guan Yu, and Zhang Fei as the three generals that lead the warring path of Shu. But unlike Wei, Shu has learned something precious over the wars that they have fought, especially for Liu Bei. He thinks that there will always be a better strategist than him to lead Shu to its victory. Thinking about recruiting the great strategist — the sleeping dragon Zhuge Liang, Shu needs to refactor…uhm…I mean reorganise their faction to be more adaptable to changing strategist.
Guess what? they do! instead of implementing directly the attack strategy inside the attack() function, Shu depends on the abstraction of the strategy and use that to create multiple strategy that can adapt to whoever strategist they wanted to use. So whoever wanted to become Shu’s strategist just need to conform to that AttackStrategy protocol and they can join the war as a new strategist in town! Isn’t this a better way?
If you read slowly, I just explained Strategy Pattern in the most historically inaccurate way possible! In the real world implementation, we usually use this pattern to abstract procedures that can change, some examples are ImageUploader, FileValidator, DataRequester, etc. To put it simply, if the steps on a procedure can change and your code would be messed up if it did, it is worth to abstract the algorithm using this pattern.
I hope I explained well and I am open to inputs to what I explained wrong. I want to disclose that I am not an avid Three Kingdoms era historian, what I am saying about the characters in the story should be fictional as I am taking a game as a reference. If you are interested in reading more about Three Kingdoms, there are so many Wikis that can explain wayyyy better than this article.
Thanks for reading! You can share this article if you like it and maybe pay a visit to my website here.