1

假设我有一个策略模式,其中有 2 个实现该策略的类,每个类都有一组值,但这些值存储在不同的容器中,比如一个向量和一个列表。

我想要一个迭代器,允许我遍历每个类中的每个值。我还希望能够使用标准迭代器方式,这意味着以这种方式遍历值:

Strategy s = //Some initialization.
Strategy :: iterator it;
for (it = s.begin(); it != s.end() ; ++it){
//Do something with *it.
}

目前我唯一的解决方案是:另一种策略模式,其中 Strategy 类具有上下文迭代器。另一个策略迭代器和每个“策略实现类”都有它自己的 - 类 selfIterator :公共策略迭代器。

但这似乎很麻烦,也许别人比我更有创造力。所以我真的很感激一些想法:),谢谢!

它适用于操作多项式的程序,其中策略是多项式的表示,一个带有向量,另一个带有列表的策略。

这也是我要用于的程序的 H 文件:

#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 */
4

0 回答 0