Kevin Kahl wrote:Let's change gears for a second and think about an RPG. Let's say you have a NPC boss. We can program that boss to implement strategies. A ranged spell attack and a close melee attack. During the course of a 'boss battle', the boss changes its behavior from ranged spells to close melee attacks? Is this right?
You've lost me. I'm not much of an RPGer so I can't really relate to what you describe.
As for your powers example, like I said, it doesn't jump out at me as an implementation of Strategy even though some of the form may be there. The other thing with Strategy is that there is always a context that the strategy will base its behavior on. In the football example, the players on the team are the context, and possibly the current field position. In the pay calculation example, the context is the employee type. The context is always external to the Strategy itself. I suppose in your heroes example, the hero provides the context but looking at your PowerBehavior implementations, there's nothing in them that uses the hero's attributes to influence the behavior. A Strategy is always going to be coupled in some way to the context in which it is used. In your code, your Powers are fully independent of the context. That's what's missing in your case.
Another example would be a hypothetical SortAnalyzer class whose responsibility is to analyze a SortingAlgorithm. The SortAnalyzer will report on how long it took to sort an array using different SortingAlgorithms. First, you give the analyzer an Array to sort (the context). Then you will give it a SortingAlgorithm (the Strategy) to use. The Strategy has a dependency on the SortAnalyzer in that it expects the SortAnalyzer to provide the data to be sorted.
I hope that makes more sense.