170 likes | 195 Views
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 iterate through them in multiple ways (forwards, backwards, skipping, or depending on values in the structures).
E N D
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 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.
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 don’t have to expose any underlying details about the list data if we don’t need to.
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.
How do they work together? Aggregate Client Iterator CreateIterator First() Next() IsDone() CurrentItem() Implements Implements ConcreteAggregate ConcreteIterator CreateIterator
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?
Implementation • Example in .NET • IEnumerable • GetEnumerator • IEnumerator • Object Current • bool MoveNext() • void Reset()
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
On to the next pattern! • But first, any questions about iterator?
Your Situation • You want to provide a way to save the state of an object, without violating encapsulation
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.
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
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.
Consequences • Preserving encapsulation boundaries. (good) • Simplifies originator (good) • Could be expensive (bad) • Defining narrow and wide interfaces (hard) • Bulks up the caretaker (bad)
Related Classes • Command: can use mementos to maintain state for undoable commands • Iterator : Mementos can be used for iteration
Well? • Everybody get it? • Any questions?