Programming Pattern Iterator Pattern

advertisement
Programming Pattern
Iterator Pattern
ITERATOR Programming Pattern
• INTENT:
•Provide a method to access elements of a collection
without exposing the underlying implementation.
•Allows you to traverse the elements, select one, find
the first, last, a particular index, etc.
MOTIVATION
• Collections should be able to be traversed
WITHOUT exposing the internal structure of the
collection.
•It should be able to have different methods of
traversals.
ITERATOR Programming Pattern
This is the collaboration of the pattern – A LIST with the iterator attached
attached.
If you have the list in your domain,
you will probably have many times you want to iterate thru it..
WRITE ONCE – REUSE OFTEN
ITERATOR Programming Pattern
Typical client code:
...
List list = new List();
...
ListIterator iterator = new ListIterator(list);
iterator.First();
while (!iterator.IsDone()) {
Object item = iterator.CurrentItem();
// Code here to process item.
iterator.Next();
}
Download