Advertisement

Strategy pattern.... am I on the right track?

Started by September 11, 2013 12:18 AM
12 comments, last by Nypyren 11 years, 5 months ago


I understand and can appreciate your points. The wisdom of when to use a pattern is something that's acquired through experience. It's hard to make a simple and complete example for something that requires a certain degree of complexity to explain. In responding to the OP, I can only assume that the choice has been made as to warrant the use of a pattern. Saying that, I'll certainly adopt code if it offers material benefit.

Actually, the example is arbitrary. I'm not trying to force fit the Strategy pattern in my code (existing or new). I'm actually just wanting to understand the Strategy pattern and see if my example is correct, in the right area, close but no cigar, or just wrong. Of course, I'm looking for feedback or sample code on how to make it better.

Beginner in Game Development?  Read here. And read here.

 


Actually, the example is arbitrary. I'm not trying to force fit the Strategy pattern in my code (existing or new). I'm actually just wanting to understand the Strategy pattern and see if my example is correct, in the right area, close but no cigar, or just wrong. Of course, I'm looking for feedback or sample code on how to make it better.

I would say your example is far from the point.

With strategy the underlying behavior of each object implementing the strategy is different. With yours the underlying behaviors are all the same (hence why you can default construct them.

An OK example is the one used by Wikipedia, which implements a strategy for processing various types of mathematical commands like Add, Subtract, and Multiply. I suggest reading it over.

I would also strongly suggest reading the c2 wiki on strategy and state.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Advertisement

To me, Strategy is just something you get when you implement two or more algorithms that produce the same effect that can be used interchangeably, where all differences are encapsulated. If the results are different, it's not a Strategy. Interchangeable movement methods like walking vs. flying do totally different things (the flier can FLY), which violates the definition in my mind. That's polymorphism. That's not Strategy. Boyer-Moore vs. every-character string searching is a Strategy.

This topic is closed to new replies.

Advertisement