Behavioral Patterns - Strategy Pattern Exercise

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

  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.

Comments

Popular posts from this blog

Distributed Transaction Management with REST and Try Confirm/Cancel Pattern

Creating a Library Project for Spring Boot

Exception Handling in WebMethods