Sie sind auf Seite 1von 12

4.

Iterator(257)
• It is a behavioural design pattern with 257 catalog
number and used for how an aggregates elements are
accessed and traversed.
• Sequentially access the elements of a collection.
• Provide a way to access the elements of an aggregate
object sequentially without exposing its underlying
representation.
• Iterator pattern describes

1
1. Pattern Name : Iterator(257)
2. Classification : Object Behavioral Pattern
3. Intent : Provide a way to access the elements of an
aggregate object sequentially without exposing its
underlying representation. Sequentially access the
elements of a collection
4. Also Known As: Cursor
5. Motivation: An aggregate object such as a list should
give you a way to access its elements without
exposing its internal structure. Moreover, you might
want to traverse the list in different ways, depending
on what you want to accomplish.

2
3
• 6. Applicability: Use the Iterator pattern
1. to access an aggregate object's contents without
exposing its internal representation.
2. to support multiple traversals of aggregate objects.
3. to provide a uniform interface for traversing
different aggregate structures.

4
7. Structure: Class and interaction diagrams

5
• 8. Participants : Participants are
Iterator,
Concrete Iterator ,
Aggregate and
Concrete Aggregate.

6
9. Collaborations: A ConcreteIterator keeps track of the
current object in the aggregate and can compute the
succeeding object in the traversal.
• Participants are Iterator, ConcreteIterator ,Aggregate and
ConcreteAggregate classes collaborate with clients at run
time.
• 10.Consequences: The Iterator pattern has
three important consequences:
1. It supports variations in the traversal of an
aggregate.
2. Iterators simplify the Aggregate interface.
3. More than one traversal can be pending on
an aggregate.

8
11. Implementation: Iterator has many
implementation variants and alternatives. Some
important ones follow.
1. Who controls the iteration?
2. Who defines the traversal algorithm?
3. How robust is the iterator?
4. Additional Iterator operations.
5. Using polymorphic iterators in C++.
6. Iterators may have privileged access.
7. Iterators for composites.
8. Null iterators.
9
12. Sample Code: Programs in c++
1. List and Iterator interfaces.
2. Iterator subclass implementations.
3. Using the iterators.
4. Avoiding commitment to a specific list
implementation.
5. Making sure iterators get deleted.
6. An internal ListIterator.

10
• template <class Item>
class Iterator
{
public: virtual void First() = 0;
virtual void Next() = 0;
virtual bool IsDone() const = 0;
virtual Item CurrentItem()const = 0;
protected:
Iterator();
};

11
13. Known Uses: Iterators are common in object-oriented
systems. Most collection class libraries offer iterators in one
form or another.
• Popular collection class library.
• It provides both a fixed size.
• Iterators don't have to be defined as explicitly in Smalltalk.

14.Related Patterns: Related patterns are


 Composite (163),
 Factory Method (107) and
 Memento (283).

Das könnte Ihnen auch gefallen