Design patterns: Iterator and Strategy

By | April 27, 2016

Was looking at the “strategy” and “iterator” patterns today. They are both operational/behavioral patterns, which makes them different from the creational patterns like the Factory Method, for instance. What’s interesting is that they might not be that different when it comes to the implementation. It’s the usage which makes them different.

Strategy pattern is about having different ways of doing something. Factory Method is about having different ways of creating something. From this standpoint, it seems that factory method is just a specialized strategy pattern.

Either way, back to the normal-world examples..

What’s the iterator? It’s, basically, something or someone who you can ask for the next available item in a set of items, and who can give you access to all those items in the set. In that sense, a waitress in the restaurant will be iterating through your meal choices as you are having a five course meal, for example.

What’s the strategy? First of all, I’m wondering if it’s the right word for this pattern since it’s not necessarily the “strategy” we are talking about – sometimes it looks more like a tactic. However, the idea is that you may have different ways of doing something. For example, if you wanted to move to another house, you would have at least a couple of choices: you could do it yourself, or you could hire somebody to do it for you. Those would be your “strategies” as far as relocation is concerned. They are both about moving, but they assume different implementations.

Leave a Reply

Your email address will not be published.