Iterator Design Pattern C++ (under construction):
Problem:
Definition:
Provide a way to access the element sequentially.
objective/purpose:
Key Points:
- decouple algorithm and data structure.
- Need to transverse.
- Different way of transverse.
- New API like next or previous API
- provide uniform traverse.
- Without expose internal object.
- Std transverse protocol.
- access or traverse.
- generic programming (separate algorithm from data structure)
- Two hierarchy same like visitor.
- Iterator can transverse and access only but visitor can apply operation on it.
- Memento can be used into iterator to restore the previous state.
- It adds a new wrapper on container to provide the new API like next, previous.
- By adding wrapper, we are following OCP (open close principle).
- If we don’t want to use STL container in embedded device, we can create own container and can provide API with iterator pattern.
- dvantage:
Disadvantage:
Class Diagram:
Real Class Diagram Example:
Source code with real example:
Output: