Here’s a Design Pattern from ActionScript 3.0 with Design Patterns. I find myself using this utility class more and more, so I thought I’d post an example.

The Iterator pattern handles looping through lists of data. These types of tasks are very common. I find myself working on this type of thing all the time.The obvious choice is to make an Array. But this leaves you writing code each time to handle every situation that might arise. Not to mention that making these systems around an Array doesn’t provide any of the OOP goodness that makes your work easier, more reliable and easier to update.

When would use the Iterator and why? You could use the Iterator any time you had lists of things that you need to cycle through. This could be groups of MovieClips. It could be data received from the server. Dynamic gallery. It’s good any time you find yourself looping through an Array and working with all it’s elements.

The Iterator pattern is based around two classes: Collection and Iterator. The Collection class holds the data and the Iterator class access the data. Collections can be written to hold any type of data. Iterators can be written to access data in various ways from the Collection. Basic types of Iterators might access data from first to last, reverse or randomly.

The Collection class is a simple class that acts like an Array. It contains an Array as a private property to store data. The Collection provides public methods to access elements of it’s Array and a method to set the Iterator to use with the data.The Collection class must define a function to set the Iterator.

The Iterators must define the following public methods, next, hasNext and reset. The next method returns the item out of the Collection. The hasNext method returns false if you have reached the last item in your Collection. And, the reset method starts the Iterator over again.

The easiest way to work with the Collection for myself is to store Objects.Object are flexible can store any amount of data.

Leave a Reply

Your email address will not be published. Required fields are marked *