Code covered by the BSD License

Design Pattern: Iterator (Behavioural)

Bobby Nedelkovski (view profile)

04 Sep 2009 (Updated )

A MATLABĀ® OOP implementation of the Iterator Behavioural Design Pattern

Test Script for Class CellArrayListIterator

Test Script for Class CellArrayListIterator

Step through and execute this script cell-by-cell to verify the iterator for a CellArrayList.

Written by Bobby Nedelkovski MathWorks Australia Copyright 2009-2010, The MathWorks, Inc.

Clean Up

```clear classes
clc
```
```Warning: Objects of 'onCleanup' class exist.  Cannot clear this class or any of its super-classes.
```

Create 2x2 Array of Instances of CellArrayList

```myList(2,2) = CellArrayList();
```

Append Arbitrary Elements to End of All Lists

```myList.add(5);  % a single integer
```

Append Arbitrary Elements to End of Particular Groups of Lists

```myList(1,1).add(rand(2));        % a 2x2 matrix
myList(1,:).add({50,55});        % 2 integers as 2 unique elements
myList(2,:).add({rand(3),5:7});  % a 3x3 matrix and a 1x3 array
myList(:,2).add({10,11;12,13});  % a 2x2 cell array
myList(2,2).add({150,160,170});  % 3 integers as 3 unique elements
```

Display 'myList'

Alternatively, you can execute "myList.display()" which produces the same output.

```myList
```
```***List #1***
list[1]{1} =
5
list[1]{2} =
0.9961    0.4427
0.0782    0.1067
list[1]{3} =
50
list[1]{4} =
55
list[1]{5} =
2x2 CellArrayList handle with no properties.

***List #2***
list[2]{1} =
5
list[2]{2} =
0.9619    0.8173    0.3998
0.0046    0.8687    0.2599
0.7749    0.0844    0.8001
list[2]{3} =
5     6     7
list[2]{4} =
2x2 CellArrayList handle with no properties.

***List #3***
list[3]{1} =
5
list[3]{2} =
50
list[3]{3} =
55
list[3]{4}{1,1} =
10
list[3]{4}{2,1} =
12
list[3]{4}{1,2} =
11
list[3]{4}{2,2} =
13

***List #4***
list[4]{1} =
5
list[4]{2} =
0.9619    0.8173    0.3998
0.0046    0.8687    0.2599
0.7749    0.0844    0.8001
list[4]{3} =
5     6     7
list[4]{4}{1,1} =
10
list[4]{4}{2,1} =
12
list[4]{4}{1,2} =
11
list[4]{4}{2,2} =
13
list[4]{5} =
150
list[4]{6} =
160
list[4]{7} =
170

```

Create Iterator for Array of CellArrayLists

```myIter = myList.createIterator();
```

Traverse First 3 Elements in All Lists

```a = myIter.next()  % a = {5,5; 5,5}
b = myIter.next()  % b = {2x2 matrix,50; 3x3 matrix,3x3 matrix}
c = myIter.next()  % c = {50,55; 1x3 array,1x3 array}
```
```a =
[5]    [5]
[5]    [5]
b =
[2x2 double]    [        50]
[3x3 double]    [3x3 double]
c =
[        50]    [        55]
[1x3 double]    [1x3 double]
```

Reset Iterator for Each List

```myIter.reset();
```

Traverse All Elements of a Particular List

```particularIter = myIter(1,2);
while particularIter.hasNext()
elt = particularIter.next()
% ...operations to perform on elt go in here...
end
% Reset iterator.
particularIter.reset();
```
```elt =
5
elt =
50
elt =
55
elt =
[10]    [11]
[12]    [13]
```

Traverse All Elements of All Lists

```hasNext = myIter.hasNext();
while any(hasNext(:))
elts = myIter.next()
% ...operations to perform on elts go in here...
hasNext = myIter.hasNext();
end
% Reset iterators.
myIter.reset();
```
```elts =
[5]    [5]
[5]    [5]
elts =
[2x2 double]    [        50]
[3x3 double]    [3x3 double]
elts =
[        50]    [        55]
[1x3 double]    [1x3 double]
elts =
[               55]    {2x2 cell}
[2x2 CellArrayList]    {2x2 cell}
elts =
[2x2 CellArrayList]       []
[]    [150]
elts =
[]       []
[]    [160]
elts =
[]       []
[]    [170]
```

Alternative implementation of the same operation...

```n = numel(myIter);
while any(reshape(myIter.hasNext(),n,1))
elts = myIter.next()
% ...operations to perform on elts go in here...
end
```
```elts =
[5]    [5]
[5]    [5]
elts =
[2x2 double]    [        50]
[3x3 double]    [3x3 double]
elts =
[        50]    [        55]
[1x3 double]    [1x3 double]
elts =
[               55]    {2x2 cell}
[2x2 CellArrayList]    {2x2 cell}
elts =
[2x2 CellArrayList]       []
[]    [150]
elts =
[]       []
[]    [160]
elts =
[]       []
[]    [170]
```

Check End Of Traversal of All Lists

next = [0,0; 0,0] (matrix of falses)

```next = myIter.hasNext()
```
```next =
0     0
0     0
```

Try Access Next Element

This yields cell array of empty sets [ ] given we have already traversed all elements of each corresponding list.

```elts = myIter.next()
```
```elts =
[]    []
[]    []
```