Design Patterns in ActionScript-Iterator

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.

n By THE GOF BOOK

 

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.

  1. interface Iterator
  2. {
  3. function   first () : void
  4. function   next () : void
  5. function   hasNext () : Boolean
  6. function   getElement () : Object
  7. }

Also, we define the interface for data structure.

  1. public interface DataStructure
  2. {
  3. function   getIterator () : Iterator
  4. }

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 | Download Download Full Project

Enjoy!

你可能感兴趣的:(Access,performance,actionscript)