To iterate is human
Iterator versus the Enumeration Method
Enumeration Method is a general design pattern, not just a language-specific idiom. In the right situation, it offers a number of significant benefits. For example, if you have a relatively stable set of actions you want to perform during an iteration, the fact that a language does not support closures becomes less of an issue. Passing blocks as objects is particularly effective when iteration is ad hoc but, when the kinds of loop action you have are well characterised and bounded, common actions can be wrapped up and provided as predefined Command classes.
Alternatively, when traversing aggregate data structures holding different kinds of elements, such as objects that represent the syntax structure of source code or a document, Enumeration Method with a Visitor interface offers a far simpler programming model than working multiple nested loops, multiple Iterator types and explicit runtime type checking. It is also much easier to write unit tests against such structures by passing Mock Objects to the Enumeration Method.
But what of the original observation that kicked off this article? If you recall:
Whenever you mention magic numbers — three in this case — you implicitly invite others to check your working (in a discipline where we don't do nearly as much reviewing and checking as we should, this is no bad thing). The claim is that I was off by one and that, including Enumeration Method (or Internal Iterator), there are four. Given that I seem to be quite a fan of Enumeration Method, have written about it going back many years, and even mention it in the very same article, is this correction correct? Not quite, and it is worth understanding why.
Had the problem to be solved just been how to provide some mechanism for traversal that kept the collection's internal representation hidden from the caller, three would have been right out. In fact, so would four. Here's a summary of the three approaches from the previous article:
And then add Enumeration Method. And then add Batch Method. And then you can choose whether or not to count separately variations of these or combinations that draw on more than one approach, such as Batch Iterator (also known as Chunky Iterator).
However, the phrasing of the design issue was more constrained:
This constraint is important because not only does it subset the available solutions, it also highlights a significant distinction between Iterator and Enumeration Method. While many choices are driven by idiom and context, keep in mind that the detail of the problem being solved casts an important vote (or veto) in choosing an appropriate solution.
In this particular case, one of Enumeration Method's strengths — that of fully encapsulating any concept of position and iteration mechanism from the caller — is a mismatch for the problem we want to solve. What is needed are solutions that have the property of both traversal and persistent position, hence the inclusion of Iterator and the exclusion of Enumeration Method.
So, to summarise, Iterator supports traversal by encapsulating position and offering traversal control whereas Enumeration Method supports traversal by encapsulating the whole loop and all of its associated mechanisms. Specific requirements and general context help to determine which is the more appropriate solution in a given situation. ®