Design Pattern Examples
Overview of object-oriented design patterns
Iterator< TItemType > Class Template Reference

Represents an iterator for a container by implementing the IIterator interface. More...

Inheritance diagram for Iterator< TItemType >:
Inheritance graph
Collaboration diagram for Iterator< TItemType >:
Collaboration graph

Public Member Functions

 Iterator (TItemType[] items)
 Constructor.
 
TItemType? Next ()
 Returns the next item in the iteration or null, if there are no more items.
 
void Reset ()
 Reset the iterator to the beginning.
 
void Reset ()
 Start iteration from beginning of container.
 
TItemType? Next ()
 Retrieve the next item from the container.
 

Private Attributes

TItemType[] _items
 The array of items to iterate over.
 
int _index
 The index into the _items array to the next item.
 

Detailed Description

Represents an iterator for a container by implementing the IIterator interface.

This class retains a copy of the elements to be iterated over so it is immune to changes in the container from which this iterator was generated.

Template Parameters
TItemTypeThe type of each item provided by the iterator

This iterator is immune to changes in the source container. However, that makes this iterator expensive because the items being iterated over have to be duplicated in this class.

One alternative is for there to be only a single iterator and that iterator is implemented on the container class itself. If there are more than one type of iterator (as in this example) then the source container would have to implement multiple iterators, one for each type of item returned from the iterator.

Another alternative is for this iterator class to have a way to access the source container's data so the data doesn't have to be copied.

Definition at line 90 of file IteratorContainer_Class.cs.

Constructor & Destructor Documentation

◆ Iterator()

Iterator ( TItemType[]  items)
inline

Constructor.

Parameters
itemsThe items to iterate over.

Definition at line 106 of file IteratorContainer_Class.cs.

References Iterator< TItemType >._items.

Member Function Documentation

◆ Next()

TItemType? Next ( )
inline

Returns the next item in the iteration or null, if there are no more items.

Returns
The next item or null if no more items.

Implements IIterator< TItemType >.

Definition at line 116 of file IteratorContainer_Class.cs.

References Iterator< TItemType >._index, and Iterator< TItemType >._items.

◆ Reset()

void Reset ( )
inline

Reset the iterator to the beginning.

Implements IIterator< TItemType >.

Definition at line 131 of file IteratorContainer_Class.cs.

References Iterator< TItemType >._index.

Member Data Documentation

◆ _index

int _index
private

The index into the _items array to the next item.

Definition at line 100 of file IteratorContainer_Class.cs.

Referenced by Iterator< TItemType >.Next(), Iterator.Next(), Iterator< TItemType >.Reset(), and Iterator.Reset().

◆ _items

TItemType [] _items
private

The array of items to iterate over.

Definition at line 95 of file IteratorContainer_Class.cs.

Referenced by Iterator< TItemType >.Iterator(), Iterator< TItemType >.Next(), and Iterator.Next().


The documentation for this class was generated from the following file: