Summary of Iterator Pattern Usage:
Purpose: Use the Iterator pattern when you need to iterate over elements of a collection
without exposing the internal structure to the client code.
Implementation Approaches:
Custom Iterator (hasNext()/next()):
- Implement custom iterator as an inner class of the collection.
Provide methods like hasNext() and next() to control iteration over the collection’s elements.
Manages iteration state internally (e.g., index tracking for sequential access). - Using begin()/end() Methods:Leverage begin() and end() methods provided by standard containers (e.g., std::vector, std::list) in the C++ Standard LibraryReturns iterators (iterator or const_iterator) that encapsulate iteration logic (operator!=, operator++, operator*) for seamless traversal.No need for a separate inner iterator class; relies on STL iterator types.

Approach 1 Diagram

approach 2 diagram
Code 1 : Approach 1:
class Employee {
string mName;
int mId;
public :
Employee(string name, int id) {
mName = name;
mId = id;
}
string getName() {
return mName;
}
int getId() {
return mId;
}
};
//class Iterator;
class HRSystem {
private :
std::vector<Employee> mEmpVec;
public:
//class Iterator;
void addEmployee(Employee emp) {
mEmpVec.push_back(emp);
}
class Iterator {
public:
Iterator(const HRSystem& hr) : hrSystem(hr), index(0) {}
bool hasNext() const {
return index < hrSystem.mEmpVec.size();
}
const Employee& next() {
return hrSystem.mEmpVec[index++];
}
private:
const HRSystem& hrSystem;
size_t index;
};
// Method to get iterator
Iterator begin() const {
return Iterator(*this);
}
};
client code from approach 1:
int main() {
HRSystem hrs;
Employee e1("ABC", 1);
Employee e2("DEF", 2);
hrs.addEmployee(e1);
hrs.addEmployee(e2);
HRSystem::Iterator itr = hrs.begin();
while (itr.hasNext()) {
auto emp = itr.next();
cout <<" emp details :: " << emp.getId() << " " << emp.getName() <<endl;
}
}
Approach 2 to use begin/end from underylying containers.
class HRSystemCppStyle {
private :
std::vector<Employee> mEmpVec;
public:
//class Iterator;
void addEmployee(Employee emp) {
mEmpVec.push_back(emp);
}
// Method to get iterator
std::vector<Employee>::iterator begin() {
return mEmpVec.begin();
}
std::vector<Employee>::iterator end() {
return mEmpVec.end();
}
};
client code for approach 2:
int main() {
HRSystemCppStyle hrscpp;
Employee e3("JKL", 1);
Employee e4("XYZ", 2);
hrscpp.addEmployee(e3);
hrscpp.addEmployee(e4);
for (auto itr = hrscpp.begin(); itr != hrscpp.end(); itr++) {
cout <<" emp details :: " << itr->getId() << " " << itr->getName() <<endl;
}
}
Follow more posts @ https://jdecodes.wordpress.com
My all design pattern articles :
- https://jdecodes.wordpress.com/2024/07/13/builder-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/command-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/iterator-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/mediator-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/state-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/memento-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/observer-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/strategy-pattern/
- https://jdecodes.wordpress.com/2024/07/20/visitor-design-pattern/
- https://jdecodes.wordpress.com/2024/07/20/adapter-desing-pattern/
- https://jdecodes.wordpress.com/2024/07/20/bridge-design-pattern/
- https://jdecodes.wordpress.com/2024/07/22/composite-desing-pattern/
- https://jdecodes.wordpress.com/2024/07/22/facade-design-pattern/
- https://jdecodes.wordpress.com/2024/07/22/decorater-design-pattern/
Leave a comment