Understanding the Problems with Iteration


For the purposes of this early discussion of iterating over collection data, we'll use the following class to illustrate points. The customized UIntCollection class is essentially a glorified array. However, although an array can store any sort of data, the UIntCollection stores only data of type uint.

package com.peachpit.aas3wdp.collections {    public class UIntCollection {       private var _data:Array;       public function UIntCollection() {          _data = new Array();       }       public function addElement(value:uint):void {          _data.push(value);       }    } }


Iterating over collection data presents several common dilemmas. One dilemma deals with how an object allows access to the collection. One option is to simply expose the collection.

For example, if a class has an array property, it can expose the array using a getter method. Adding the following getter method to the UIntCollection class does just that:

public function get data():Array {    return _data; }


The preceding solution enables you to iterate through the collection using a for statement, as follows:

var collection:UIntCollection = new UIntCollection (); collection.addElement(1); collection.addElement(2); var i:uint; for(i = 0; i < collection.data.length; i++) {    // Code that uses collection data.    trace(collection.data[i]); }


There are two major related flaws with the preceding solution. The first is that exposing the array directly breaks encapsulation in a fundamental way. The intention of exposing the collection by way of a getter method is to enable iteration over the collection elements. However, as a consequence, it's also possible to alter the collection without the object knowing anything about it. For example, consider the following:

var collection:UIntCollection = new UIntCollection (); collection.data[0] = "one";


In this example, you can see that a value is assigned to the data collection without the object being notified. Furthermore, the example assigns a string value to the collection element even though the UIntCollection class expects that the data collection contains only unsigned integers.

Note

ActionScript does not currently support typed arrays, which is why the preceding example allows us to assign a string to an element of the array. In a language with typed arrays, you would have to declare an array of a particular type (for example, uint).


The second flaw in the preceding solution is that it exposes not only the data, but also the structure of the data. If you want to iterate over the elements of a collection, there's no reason you have to know the structure of the collection to accomplish that. In fact, having to know the structure is a hindrance because it requires different ways to iterate over different structures.

A second dilemma with iterating over collection data deals with the interface. In the preceding example, we noted that exposing collection data directly is not a good idea because it could have unexpected consequences. As a solution, it might seem like a good idea to define an API for the collection class that enables you to iterate over the collection data. For example, the following addition to the UIntCollection class enables you to loop through the elements of the collection while maintaining good encapsulation:

package com.peachpit.aas3wdp.collections {    public class UIntCollection {       private var _data:Array;       private var _index:uint;       public function UIntCollection() {          _data = new Array();          _index = 0;       }       public function addElement(value:uint):void {          _data.push(value);       }       public function reset():void {          _index = 0;       }       public function hasNext():Boolean {          return _index < _data.length;       }       public function next():uint {          return uint(_data[_index++]);       }    } }


The downside with the preceding API is that it makes the collection responsible for iterating over the data. There are at least two major flaws with that: One flaw is that each collection object maintains its own cursor (_index) so that you cannot iterate over the collection object's data more than once simultaneously. The second flaw is that the collection class has to define every possible way to iterate over the data. The preceding example assumes that you always want to iterate over the data in a forward direction, one element at a time, in the order that elements appear in the array. If you want to add methods for iterating over the collection in ascending value order or skipping every other element, you must add a lot of responsibilities to the collection class itself.




Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net