Say i have a strategy pattern with 2 classes implementing the strategy, where each has some set of values, but the values are stored in different containers, say a vector and a list.
I would like to have an iterator allowing me to go over each of the values in each class. I would also like to be able to use the standard iterator way, meaning going over the values in this manner :
Strategy s = //Some initialization.
Strategy :: iterator it;
for (it = s.begin(); it != s.end() ; ++it){
//Do something with *it.
}
for now the only solution i have is : another strategy pattern where the Strategy class has the context iterator. another strategyIterator and each "strategy implementing class" has it's own - class selfIterator : public strategyIterator.
but this seems quite a hassle, and maybe someone else has more creativity than me. So i'd really appreciate some ideas :), Thanks!
It's for a program that manipulate polynomials, where the strategy is a representation of the polynomial, one with a vector and the other strategy with a list.
Here is also the H file of the program i want to use it for :
#include <iostream>
#include <vector>
#include <list>
using namespace std;
#ifndef MYPOLY_H
#define MYPOLY_H
class PolyRep;
class RegIterator;
typedef enum Poly
{
REG_POLY = 1, SPARSE_POLY = 2
};
/* =============================================================================
* MyPoly class.
* =============================================================================*/
class MyPoly
{
public:
MyPoly(double arr[], unsigned int arrSize);
MyPoly(double num);
MyPoly(double X[], double Y[], int arrSize);
MyPoly(string sPoly);
~MyPoly();
string toString();
double evaluate(double x) const;
MyPoly derive(int n);
MyPoly & operator =(const MyPoly &rhs);
MyPoly & operator +=(const MyPoly &rhs);
MyPoly & operator -=(const MyPoly &rhs);
MyPoly & operator *=(const MyPoly &rhs);
MyPoly operator +(const MyPoly& other);
MyPoly operator -(const MyPoly& other);
MyPoly operator *(const MyPoly& other);
bool operator ==(const MyPoly& b) const;
MyPoly operator -() const;
private:
PolyRep * _poly;
};
/* =============================================================================
* PolyRep class.
* =============================================================================*/
class PolyRep
{
public:
virtual double evaluate(const double &x) const = 0;
//virtual iterator begin () = 0;
//virtual PolyRep * derive(int n) = 0;
protected:
int _degree;
};
/* =============================================================================
* RegPoly class.
* =============================================================================*/
class RegPoly : public PolyRep
{
public:
RegPoly(double arr[], int degree);
~RegPoly();
double evaluate(const double &x) const;
private:
vector <double> _values;
};
/* =============================================================================
* SparsePoly class.
* =============================================================================*/
class SparsePoly : public PolyRep
{
public:
SparsePoly(double arr[], int degree);
~SparsePoly();
double evaluate(const double &x) const;
private:
/*A class to represent a node in the SaprsePoly list.*/
class SparseNode
{
public:
SparseNode(int n = 0, double value = 0);
int getN() const;
double getValue() const;
private:
int _n;
double _value;
};
/*End of sparse node class.*/
list <SparseNode*> _values;
};
#endif /* MYPOLY_H */