Race model from the popular game World of Warcraft.

"> Behavioral Patterns - Strategy Pattern Exercise - Zhenyang Hua
This is a space that holds a collection of my personal work and ideas

Behavioral Patterns - Strategy Pattern Exercise

Posted on 07/03/2018

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

  1. https://en.wikipedia.org/wiki/Strategy_pattern (15 minutes reading)

Practice Materials

  1. 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:

  1. Context (The abstract class that defines the base of all races) - Race
  2. Concrete Context (The implementations of the Race) - Human, Orc, Tauren
  3. Strategy (Different behavior interfaces) - DanceBehavior, FightBehavior.
  4. 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

  1. What are the common use cases to apply a Strategy Pattern?
  2. What are the OOP design principles used in this exercise?
  • Encapsulate what varies
  • Favor composition over inheritance
  • Program to interfaces, not implementations.

Read on GitHub