Iterator Pattern Files
I finally cleaned up the files I showed for my Iterator presentation a few weeks ago. Here’s the code. All of these examples are basically Hello Worlds. They should be pretty easy to understand, but, they’re not that exciting.
The main class is IteratorTalkMain. Take a look at the init() method and you can comment in/out various methods that demonstrate different aspects of iterator.
The code demonstrates the following:
1. Simple iterator.
2. Iterating on an interval without having to add much to your main class.
3. Iterate using the remove() method. (Earlier methods shown are hasNext() and next())
4. Iterating over a hash array. Because your iterator class is external to the class that is calling the iteration, the class that calls the iteration is able to treat a hash identically to an array.
5. Using different iterators to traverse the same data. This example filters out strings that do not have a minimum length.
6. Internal iteration. For internal iterations, you pass a method to the class that holds the data, the data class traverses over its data and modifies the data using the method passed. I am not sure if I implemented this correctly. In general, it seems that internal iterators are frowned upon (at least in the GoF book).
7. Composite Iterator. This is the most complex. It shows how to iterate over a data structure that uses the Composite Pattern. In Composite, both nodes and leaves are treated identically. The iterator is recursive. There is one weird glitch in the CompositeIterator class. Everything works, but, I ended up having the iterator creation increment by 2 when I used push (which froze the player) - to fix it I added a klugey length check. If you want to take a look - look for the ‘wtf’ comment.
Most of this code was based on code from Heads Up Design Patterns, which is a great book. I also read the Iterator chapter in the Gang of Four book.
I am not a design patterns expert. If you find errors in this code, let me know.
In addition to the code, there is a swf of the presentation - basically text slides.
This code is open source. Use for whatever you want.
flickr API Change
flickr has changed their public api. Unfortunately, it affects everyone who’s made a Flash/Flickr tool. Mario has good summary here.
So, findr users - uh, it might not work. But, I’ll get to it soon.
Damn public apis.
UPDATE: August 20, 2006
I’ve updated findr to the new api location. If you are using flashr 0.4, you only need to change 2 lines in the flickr.as file
private var _AUTH_ENDPOINT:String = "http://flickr.com/services/auth/";
change to...
private var _AUTH_ENDPOINT:String = "http://api.flickr.com/services/auth/";
...and...
_REST_ENDPOINT = "http://www.flickr.com/services/rest/";
change to...
_REST_ENDPOINT = "http://api.flickr.com/services/rest/";
The updated source for flashr is here.
So, small change - but, pain in the ass because I have multiple versions of findr. I did finally get around to upgrading the detection script to swfObject - so you no longer have to click on findr to activate it in MIE. SwfObject is pretty nice, btw. In addition to handling the MIE problem, it easily allows you to pass variables from url addresses to Flash.
_lockroot for Combo Boxes
Another fun combo box bug. This fix was in LiveDocs. But, as this blog is really the best place for me to keep track of this stuff, I’m posting it here.
I couldn’t get this to happen in a simple test, so no sample swf.
Here’s the problem…
When you open a combo box, it shows up behind other objects on stage.
The solution - set the mc that the combo is attached to’s _lockroot property to true.
I never use _lockroot (because _root is evil), but, in this case, hey, it works.
Sample code:
import mx.controls.ComboBox;
var myCombo:ComboBox;
var target:MovieClip;
var d:Number=target.getNextHighestDepth();
var params:Object={_x:30, _y:20};
target._lockroot=true;
myCombo = target.createClassObject (ComboBox, 'cb' + d + '_cb', d, params);
UPDATE August 21, 2006
Unfortunately, this does not always work. And I haven’t figured out a solution other than making sure my mcs above (lower _y values - closer to the top of the screen) have higher depths than those below (greater _y values).
Male Female Sex Partner Viz
Nice to see Keith Peters doing some data visualization. (It’s work safe.)