mirror of
https://github.com/JakubVojvoda/design-patterns-cpp.git
synced 2025-12-17 21:04:36 +03:00
add Iterator pattern
This commit is contained in:
132
iterator/Iterator.cpp
Normal file
132
iterator/Iterator.cpp
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* C++ Design Patterns: Iterator
|
||||||
|
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
|
||||||
|
* 2016
|
||||||
|
*
|
||||||
|
* Source code is licensed under MIT License
|
||||||
|
* (for more details see LICENSE)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Iterator;
|
||||||
|
class ConcreteAggregate;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Aggregate
|
||||||
|
* defines an interface for aggregates and it decouples your
|
||||||
|
* client from the implementation of your collection of objects
|
||||||
|
*/
|
||||||
|
class Aggregate {
|
||||||
|
public:
|
||||||
|
virtual Iterator *createIterator() = 0;
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Concrete Aggregate
|
||||||
|
* has a collection of objects and implements the method
|
||||||
|
* that returns an Iterator for its collection
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class ConcreteAggregate : public Aggregate {
|
||||||
|
public:
|
||||||
|
ConcreteAggregate(const int size) {
|
||||||
|
list = new int[size]();
|
||||||
|
count = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterator *createIterator();
|
||||||
|
|
||||||
|
~ConcreteAggregate() {
|
||||||
|
delete[] list;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int size() const {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
int at(unsigned int index) {
|
||||||
|
return list[index];
|
||||||
|
}
|
||||||
|
// ...
|
||||||
|
|
||||||
|
private:
|
||||||
|
int *list;
|
||||||
|
unsigned int count;
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Iterator
|
||||||
|
* provides the interface that all iterators must implement and
|
||||||
|
* a set of methods for traversing over elements
|
||||||
|
*/
|
||||||
|
class Iterator {
|
||||||
|
public:
|
||||||
|
virtual ~Iterator() { /* ... */ }
|
||||||
|
|
||||||
|
virtual void first() = 0;
|
||||||
|
virtual void next() = 0;
|
||||||
|
virtual bool isDone() const = 0;
|
||||||
|
virtual int currentItem() const = 0;
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Concrete Iterator
|
||||||
|
* implements the interface and is responsible for managing
|
||||||
|
* the current position of the iterator
|
||||||
|
*/
|
||||||
|
class ConcreteIterator : public Iterator {
|
||||||
|
public:
|
||||||
|
ConcreteIterator(ConcreteAggregate *l)
|
||||||
|
: list(l), index(0) {}
|
||||||
|
|
||||||
|
void first() {
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void next() {
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isDone() const {
|
||||||
|
return (index >= list->size());
|
||||||
|
}
|
||||||
|
|
||||||
|
int currentItem() const {
|
||||||
|
if (isDone()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return list->at(index);
|
||||||
|
}
|
||||||
|
// ...
|
||||||
|
|
||||||
|
private:
|
||||||
|
ConcreteAggregate *list;
|
||||||
|
unsigned int index;
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
Iterator *ConcreteAggregate::createIterator() {
|
||||||
|
return new ConcreteIterator(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
unsigned int size = 5;
|
||||||
|
ConcreteAggregate list = ConcreteAggregate(size);
|
||||||
|
|
||||||
|
Iterator *it = list.createIterator();
|
||||||
|
for ( ; !it->isDone(); it->next()) {
|
||||||
|
std::cout << "Item value: " << it->currentItem() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
delete it;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
12
iterator/README.md
Normal file
12
iterator/README.md
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
## Iterator
|
||||||
|
|
||||||
|
Iterator pattern has behavioral purpose and applies to objects. The pattern provides
|
||||||
|
a way to access the elements of an aggregate object sequentially without exposing
|
||||||
|
its underlying representation.
|
||||||
|
|
||||||
|
### When to use
|
||||||
|
|
||||||
|
* to access an aggregate object's contents without exposing its internal representation
|
||||||
|
* to support multiple traversals of aggregate objects
|
||||||
|
* to provide a uniform interface for traversing different aggregate structures
|
||||||
|
(to support polymorphic iteration)
|
||||||
Reference in New Issue
Block a user