The iterator and memento patterns - PowerPoint PPT Presentation

About This Presentation
Title:

The iterator and memento patterns

Description:

The iterator and memento patterns Presented by Jerry Hoff Your Situation You have a group of objects, and you want to iterate through them. Also, you may want to ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 17
Provided by: Jer899
Category:

less

Transcript and Presenter's Notes

Title: The iterator and memento patterns


1
The iterator and memento patterns
  • Presented by
  • Jerry Hoff

2
Your Situation
  • You have a group of objects, and you want to
    iterate through them.
  • Also, you may want to iterate through them in
    multiple ways (forwards, backwards, skipping, or
    depending on values in the structures).
  • You might even want to iterate through the list
    of objects simultaneously using your two or more
    of your multiple ways.

3
How can we do this?
  • In object-oriented programming, an iterator is an
    object allowing one to sequence through all of
    the elements or parts contained in some other
    object, typically a container or list. An
    iterator is sometimes called a cursor, especially
    within the context of a database. From
    en.wikipedia.org/wiki/Iterator
  • The iterator pattern gives us a way to do this by
    separating the implementation of the iteration
    from the list itself.
  • The Iterator is a known interface, so we dont
    have to expose any underlying details about the
    list data if we dont need to.

4
Who is involved in this?
  • Iterator Interface
  • Simple methods for traversing elements in the
    list
  • Concrete Iterator
  • A class that implements Iterator
  • Iteratee Interface
  • Defines an interface for creating the list that
    will be iterated
  • Concrete Iteratee
  • Creates a concrete iterator for its data type.

5
How do they work together?
Aggregate
Iterator
Client
CreateIterator
First() Next() IsDone() CurrentItem()
Implements
Implements
ConcreteAggregate
ConcreteIterator
CreateIterator
6
Consequences
  • Who controls the iteration?
  • Internal and external iterators
  • Who defines the algorithm?
  • Can be stored in the iterator or iteratee
  • Robustness
  • Additional Iterator Operators
  • Previous, SkipTo
  • Iterators have privileged access to data?

7
Implementation
  • Example in .NET
  • IEnumerable
  • GetEnumerator
  • IEnumerator
  • Object Current
  • bool MoveNext()
  • void Reset()

8
Related Patterns
  • Composite
  • Iterators can be applied to recursive structures
  • Factory Method
  • To produce specific iterator subclass
  • Memento
  • Used with the iterator, captures the state of an
    iteration

9
On to the next pattern!
  • But first, any questions about iterator?

10
Your Situation
  • You want to provide a way to save the state of an
    object, without violating encapsulation

11
Who's involved?
  • Originator The object whose state we are going
    to preserve. Can use memento to restore his
    state.
  • Memento The object who saves the state of the
    Originator. Can hold a bit or all of
    originator's data.There is stuff only caretaker
    can see (the narrow interface), and stuff that
    originator can see (wide interface).
  • Caretaker The object who holds the Memento and
    possibly returns it to the originator. Doesn't
    look into the memento.

12
How do these guys work together?
Makes
Originator
Memento
SetMemento(Memento m) CreateMemento()
GetState() SetState()
state
state
Originator can set And get mememto(s)
Caretaker
13
When do I use this again?
  • you want to save a snapshot of an object's state
    - so it can be restored later
  • AND
  • directly exposing those values would expose
    implementation details - violating encapsulation.

14
Consequences
  • Preserving encapsulation boundaries. (good)
  • Simplifies originator (good)
  • Could be expensive (bad)
  • Defining narrow and wide interfaces (hard)
  • Bulks up the caretaker (bad)

15
Related Classes
  • Command can use mementos to maintain state for
    undoable commands
  • Iterator Mementos can be used for iteration

16
Well?
  • Everybody get it?
  • Any questions?
Write a Comment
User Comments (0)
About PowerShow.com