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.

clip_image001

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-256x256Demo | DownloadDownload Full Project

Enjoy!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值