静的 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;
}
長所:
- アルゴリズムのユーザーは内部インターフェースを見ることができません
短所:
- ユーザーはファクトリ メソッドを使用してインスタンスを作成する必要があります
- ライブラリ内からシークレット パラメーターにアクセスする場合は、Algorithm を AlgorithmPrivate にダウンキャストする必要があります。
私が達成しようとしていることを理解していただければ幸いです。何か提案があればお待ちしております。