QtDesignPatterns/iteratorpattern/interface.h

62 lines
1.1 KiB
C
Raw Permalink Normal View History

2024-02-06 17:21:37 +08:00
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Iterator
{
public:
Iterator(){}
virtual ~Iterator(){}
virtual bool hasNext() = 0;
virtual string next() = 0;
};
class NameIterator: public Iterator
{
public:
NameIterator(size_t index, std::vector<string> vector) : index(index), vector(vector){}
bool hasNext()
{
if(index < vector.size() - 1)
{
return true;
}
return false;
}
string next()
{
if(this->hasNext())
{
return vector.at(index++);
}
return nullptr;
}
private:
size_t index;
std::vector<string> vector;
};
class Container
{
public:
Container(){}
virtual ~Container(){}
virtual Iterator* getIterator() = 0;
};
class NameRepository: public Container
{
public:
NameRepository(size_t index, std::vector<string> vector) : index(index), vector(vector){}
Iterator* getIterator()
{
return new NameIterator(index, vector);
}
private:
size_t index;
std::vector<string> vector;
};