18 July 2012

Strategy Pattern

Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
Head First Design Patterns

If we were to use this pattern to associate a Super Hero’s talents and powers to the hero themselves it might look like the diagram below.

Strategy Pattern
Strategy Pattern

Experience tells me I would store the powers in a persistence layer (database) and would need something more flexible. Such as a name attribute for talents and powers that would let me use one class each for talents and powers.

tags: design patterns

Less Is More