Hi.
So I want to be able to implement some kind of interface that allows me to abstract the specific implementation of the storage I’m using. I’m having it store ‘Pin’ Objects (which have a name property among other things)
eg
public interface IPinMap
{
function getPin(name:String):IPin
function hasPin(name:String):Boolean
function addPin(pin:IPin):void
function removePin(pin:IPin):void
}
Now, my original implementation was simply extending flash.utils.Dictionary, but then I decided to make it more abstract/domain-specific by adding a few methods like hasPin etc (instead of using the *hasProperty *of Dictionary, which doesn’t make much sense in the domain of Pins) then this made me think I should abstract the whole ■■■■ thing and just use a private Dictionary member as the storage in case later I want to switch to using Vectors or something else (eg as3ds HashTables or something) as the storage medium.
and this is fine, but I’ve come across a problem: Iterating over this set of data. I’d prefer not to have to create a Javaesque Iterator object for this, as that is fairly unconventional in the actionscript world.
Ideally, I could loop by doing something like:
var pinMap:PinMap = new PinMap();
pinMap.addPin(new Pin("apple"));
pinMap.addPin(new Pin("orange"));
pinMap.addPin(new Pin("banana"));
for each (var pin:Pin in pinMap) {
trace(pin.name);
}
Now I know this is possible through extending [URL=“http://livedocs.adobe.com/flex/3/langref/flash/utils/Proxy.html”]Proxy and using it’s
**nextName
nextNameIndex
nextValue
**
functions.
but **I have no idea how to go about implementing these on a Dictionary **object, because it doesn’t have a numeric index…
do I have to do something like:
[LIST=1]
[]use a foreach loop over the dictionary till i find the private static var _currentElement (if none, return the first element returned by foreach)
[]move one foreach loop step forward to the next element
[]store that in _currentElement
[]return _currentElement
[/LIST]
See the problem with this is that there’s a lot of thrashing going on when foreaching over the data:
EG to loop over a set of length n, I have to do n! (factorial) loops over the ■■■■ thing, which is clearly not very efficient.
Should I switch to index-based storage, or is there a way to get around this? Another solution?
Thanks.