2. team
Subhamsekhar
Panda
BSc Computer Science
Ramniranjan Jhunjhunwala
College
Hrishikesh
Parkar
BSc Computer Science
Vidyalankar College
Sahil
Panchbhaiya
BSc Physics
Thakur College of
Science
38 39
Design
pattern
types
Iterator
story
proble
m
4
0
3. Design
pattern
• Design patterns are typical solutions to common problems in
software design. Each pattern is like a blueprint that you can
customize to solve a particular design problem in your code.
• A design pattern isn't a finished design that can be
transformed directly into code. It is a description or template
for how to solve a problem that
can be used in many different situations.
• You need to implement or modify it according to your need
for the problem.
Design patterns differ by their complexity, level of detail
and scale of applicability. In addition, they can be
categorized by their intent and divided into three groups.
types
Iterator
story
m
5. Iterator
story
problem
solution What is ITERATOR PATTERN
? Iterator is a behavioral design pattern that lets you traverse elements of a
collection without exposing its underlying representation (list, stack, tree,
etc.).
Sequentially access elements of collection.
Provide a way to access the elements of an aggregate object sequentially
without exposing its underlying representation.
The C++ and Java standard library abstraction that makes it possible to
decouple collection classes and algorithms.
Promote to "full object status" the traversal of a collection.
Polymorphic traversal.
17. problem
Collections are one of the most used data types in programming.
What about complex data structure such as trees?
18. The main idea of the Iterator pattern is to extract the traversal behavior of a collection into a
separate object called an iterator. In addition to implementing the algorithm itself, an iterator object
encapsulates all of the traversal details,
solution
20. STRUCTURE:
The Iterator interface
declares the operations
required for traversing a
collection:
Concrete Iterators
implement specific
algorithms for traversing
a collection. The iterator
object should track the
traversal progress on its
own. This allows several
iterators to traverse the
same collection
independently of each
other.
The Collection
interface declares
one or multiple
methods for getting
iterators compatible
with the collection.
Concrete
Collections return
new instances of a
particular concrete
iterator class each
time the client
requests one.
23. Pros
and
cons
Single Responsibility Principle.
You can clean up the client code
and the collections by extracting
bulky traversal algorithms into
separate classes.
Open/Closed Principle. You can
implement new types of
collections and iterators and pass
them to existing code without
breaking anything.
You can iterate over the same
collection in parallel because
each iterator object contains its
own iteration state.
Applying the pattern can be an
overkill if your app only works
with simple collections.
Using an iterator may be less
efficient than going through
elements of some specialized
collections directly.