Design Patterns in ActionScript-Iterator

chat-japonais-256x256 There is a famous saying in computer science, “Program = Data Structure + Algorithm”. It figures out the importance of how to organize the data and how to deal with the data.

Maybe, in your applications, you care more about how to show the data, because this has much to do with the user experience. However, how to organize the data and deal with the data is also or much more important, because this has much to do with the performance.

Eh, I don’t want to talk about the performance, it is too big. I just want to talk something has a little to do with how to organize the data. In our daily programming, we will use array, list, or set to store the data. If you want to know the differences between these structures, you’d better to find a textbook in this field.

Array, list and set have their own way to store the data. The array uses continual space to store the data, while the other two using disjunction space to store the data. The difference in storing the data, leads the difference on locating the data. Find the n-th element in array will be much different from the same operation in list or set.

In general, we will write some concrete class for these structures. But, sometimes, we need to access the data without considering the concrete data structure. Or sometimes, we need to define another method for traversing the data.

If you have the requirements like mentioned above, I think you can take a look at this pattern, Iterator. The intent is as follows.

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.


Here, I will show you a simple example to illustrate this pattern. In this example, I will just use the array, and provide a backward traverse.

The definition of Iterator is as follows.

interface Iterator
function first():void
function next():void
function hasNext():Boolean
function getElement():Object

Also, we define the interface for data structure.

public interface DataStructure
function getIterator():Iterator

Now, we need to implement the concrete class of the data structure and the iterator. Actually, the concrete iterator knows the details of the specific data structure. And how to traverse the data is depends on the concrete iterator’s implementation. See the code for more information, ‘coz I don’t want to put so much code here :)

Here is the class diagram.


In this pattern, we can traverse the data without considering the concrete data structure by using the same interface, further more, we can change the way of traversing the data by changing the concrete Iterator class.

Search-256x256 Demo | DownloadDownload Full Project


Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

One Response to “Design Patterns in ActionScript-Iterator”

  1. Hi,
    I like your series. A nice way to revise the basics, an a pointer for those starting out.

Leave a Reply