对于某些类的静态C++图书馆我想提供不同的接口用户的图书馆和图书馆本身。

一个例子:

class Algorithm {

  public:

    // method for the user of the library
    void compute(const Data& data, Result& result) const;


    // method that I use only from other classes of the library
    // that I would like to hide from the external interface
    void setSecretParam(double aParam);

  private:

    double m_Param;
}

我第一次尝试是建立的外部界面作为一个ABC:

class Algorithm {

  public:

    // factory method that creates instances of AlgorithmPrivate
    static Algorithm* create();

    virtual void compute(const Data& data, Result& result) const = 0;
}

class AlgorithmPrivate : public Algorithm {

  public:

    void compute(const Data& data, Result& result) const;

    void setSecretParam(double aParam);

  private:

    double m_Param;
}

赞成:

  • 用户算法不能看到内部接口

缺点:

  • 用户已经使用的一个工厂的方法,以创建实例
  • 我要垂头丧气的算法AlgorithmPrivate时,我想访问的秘密参数的内部图书馆。

我希望你明白我怎么努力来实现的,我很期待的任何建议。

有帮助吗?

解决方案

最简单的方式可能做出 setSecretParam() private 和做下一个 friendAlgorithm:

void setSecretParam(Algorithm& algorithm, double aParam)
{
  void setSecretParam(double aParam);
}

其他提示

"通常的嫌疑人"来替代继承是 桥模式.你可以定义的一个层次结构中的"小鬼"衍生从抽象的类AlgorithmImp和只能获得适当的算法在图书馆头。然后一个算法的实例可以作为创建

ConcreteAlgorithm ca1(SomeParam, new LibraryUserAlgorithm());
ConcreteAlgorithm ca2(SomeParam, new InternalAlgorithm());
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top