File Exchange

image thumbnail

Design Pattern: Iterator (Behavioural)

version (35.7 KB) by

A MATLAB® OOP implementation of the Iterator Behavioural Design Pattern

1 Download


View License

Intent: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation [1]. This design pattern is also known as Cursor.
Motivation: An example of an aggregate object is an instance of the List ADT. Consequently, an iterator can be used to traverse the elements of a list with a set of high-level abstract operations. These operations may be implemented within the List ADT but as Gamma et al.[1] wonderfully puts it, the key idea in this pattern is to take the responsibility for access and traversal out of the list object and put it into an iterator object - given the iterator describes behaviour whilst the list describes a collection.

Implementation: The Iterator abstract class is used purely to specify the requirements of its concrete implementation. This abstraction could be implemented as an external/active or internal/passive iterator -
External := the onus is on the client to advance the traversal and request next elements.
Internal := the client can supply an operation to the iterator to perform over every element of a collection

Refer to pp.257-271 Gamma et al.[1] for more information on the Iterator (Behavioural) Design Pattern.

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

[1] Gamma, E., Helm, R., Johnson, R. and Vlissides, J. Design Patterns : Elements of Reusable Object-Oriented Software. Boston: Addison-Wesley, 1995.

Comments and Ratings (3)


Zaki (view profile)

Thanks a lot for this. It really helps to see how interfaces are built and enforced in MATLAB

Thank you for your feedback Kun-Chul.

To provide clarification to the comment "good implementation for the oo design principle: polymorphism!!":

The CellArrayList implementation of the List ADT provides 'polymorphism' at the element level as I've indicated in File Exchange item #25024 by stating that CellArrayList is a storage container for a *heterogeneous* set of elements.

On the other hand, polymorphism at the list object creation level can't be generally enforced since MATLAB is loosly typed. Ideally, to conform with good software engineering practice, it would be beneficial to declare an identifier using an interface/abstract class so you just need to change the instantiating class if you simply want to switch from one implementation of List to another (i.e. you cannot perform "List myList = CellArrayList();" so then you would simply need to modify this line to use another implementation "List myList = AnotherImplList();"). So in this case, polymorphism is not apparent in MATLAB OOP.

On the alternative implementation of method 'locationsOf' (i.e. "locs = find(cellfun('isclass',obj.list,class(elt)));"), I have asserted in the comments for 'locationsOf' in abstract class 'List.m' that it should return the locations of all occurances of a single element in the list; speed shouldn't be necessarily the issue here. Hence why I use a strict equality test in "locs = find(cellfun(@(c)isequal(c,elt),obj.list));" *not* simply checking the element type. For example, if I define a list of the sort "x = [1,2,{3,4;5,6},{7,8;9,10},11]" and test x.locationsOf(2), the 'isequal' implementations yields the correct result of 2. The 'isclass' implementation will yield the relative locations of elements 1,2 and 11 given their data type matches the data type of the input argument. This is then contrary to the "contractual agreement" defined in the comments for 'locationsOf' in the abstract class 'List.m'. Sure, though you are welcome to have specialised methods in your custom subclasses of the List ADT - for example, perhaps an 'isclass' operation could be be encapsulated in a method called 'classTypeLocationsOf'.

Finally, to comment on "I hope Mathworks to develop cell array vectorization for matlab oop...", the idea of this CellArrayList realisation of the List ADT and OOP in general is to hide the low-level detail in implementation of certain functionality (like performing "find(cellarray..." or even for that matter "find(listObj==obj)" as suggested) since the user of CellArrayList only needs to know how to use 'locationsOf'. So the onus is on the developer to create their own implementations of specific class methods using base MATLAB functionality; therefore OOP shouldn't be seen as a replacement for or thought of as superseding base MATLAB functionality since functions like 'cellfun' still have their place.


good implementation for the oo design principle: polymorphism!!

I also used cell array for the objects container ( list container)
for the objects from different classes (but, those different classes inhereted from a same super class)

cell list class is more useful and powerful for the object list container.

however, try this code for the 'locationOf' method (much faster):
locs = find(cellfun('isclass',obj.list,class(elt))
, since cellfun is slow when suing function handle
(but, know the limitation of the code above:
if obj.list contains the object from a same class, then 'find' function returns matching index array.

I hope Mathworks to develop cell array vectorization for matlab oop

for example, objList is cell array contain

find(objList==obj) %this function does not support for cell array

while find(objArray==obj) is supported when objArray is array container of the object from a same class.


Updated license


Allow creation of N-D arrays of CellArrayListIterators - this is due to the changes in CellArrayList (see File Exchange item #25024). UML Diagram has been updated to reflect the change.


Abstract properties in abstract classes List and Iterator changed to concrete protected - conforms with R2009b OOP. Updated UML diagram to reflect this change. Bug fix with remove() method in CellArrayList.


Removed details on the implementation from the 'Description' field as this can be found in 'CellArrayListIterator.m' and included references to the files of interest.

MATLAB Release
MATLAB 7.10 (R2010a)

Inspired by: Data Structure: A Cell Array List Container

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video