Behavioral Patterns - Strategy Pattern Exercise
The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. In this exercise, we will use the Strategy Pattern to create a simplified Race model from the popular game World of Warcraft.
Reading Materials
- https://en.wikipedia.org/wiki/Strategy_pattern (15 minutes reading)
Practice Materials
- Use the Strategy Pattern to create a Race Model that meets the following requirements:
- It defines the race traits which varies by each race
- It has the following two behaviors that could be considered strategies.
- Dance
- Fight
- Implement at least 2 races with different behaviors
Components:
- Context (The abstract class that defines the base of all races) -
Race
- Concrete Context (The implementations of the Race) -
Human
,Orc
,Tauren
- Strategy (Different behavior interfaces) -
DanceBehavior
,FightBehavior
. - Concrete Strategy (The interchangeable strategies that could be passed in runtime) -
SwingDance
,IrishDance
,SpellFight
,TaichiFight
,BoxFight
.
Tips
- If you have never played the World of Warcraft and if you have time and interest in the mid-earth world, it is worth your time.
- You could find races from the game on the official site
Solution
World of Warcraft Race Model Example
Questions to discuss
- What are the common use cases to apply a Strategy Pattern?
- What are the OOP design principles used in this exercise?
- Encapsulate what varies
- Favor composition over inheritance
- Program to interfaces, not implementations.
Comments
Post a Comment