2 Replies Latest reply on Oct 18, 2007 4:01 AM by zz_james

    quick question on ArrayCollection class


      This is a quick question which I am sure has a simple yes or no answer.

      I was reading this blog post:

      titled 'Common Design Pattern Implementations in ActionScript 3:'

      and was looking at the AS3 iterator pattern. Am I right in thinking that if I use the mx:collections:ArrayCollection class from the flex framework, I already have an implementation of iterator pattern in AS3, or is there some confusion on my part to think this?

      Thankyou for you time

      James Smith
        • 1. Re: quick question on ArrayCollection class
          atta707 Level 2
          Strictly speaking ArrayCollection doesn't implement and Iterator. That is there is no interface in the hierarchy that it implements or any class that it extends that provides common iterator methods.

          This class does inherit createCursor() method from ICollectionView interface which returns an instance of IViewCursor which, as far as I know, is the closest it comes to iterators.

          Then of course ActionScript has a built-in collection 'iterator' in the form of 'for each' loop!

          I hope it makes some sense that some experienced folks can elaborate this topic further.

          • 2. Re: quick question on ArrayCollection class
            zz_james Level 1
            ok. I am starting to understand!.

            The iterator pattern (in the books) suggests implementing an iterator interface with these 5 methods.

            interface Iterator
            // Returns the current value
            function current();

            // Returns the current key
            function key();

            // Moves the internal pointer to the next element
            function next();

            // Moves the internal pointer to the previous element
            function previous();

            // If the current element is valid (boolean)
            function valid();

            IViewCursor whose methods ArrayCollection inherits has (amongst many others) these methods

            current : Object
            [read-only] Provides access the object at the location in the source collection referenced by this cursor.

            Moves the cursor to the next item within the collection.

            Moves the cursor to the previous item within the collection.

            using these you can, in the main, achieve the same things, although it's not right to say it 'uses the iterator pattern' - I suppose as usual the advice it 'use your own judgement - don't rely on the pattern handbook'