Reputation: 315
For some classes of a static C++ library I want to offer different interfaces for the user of the library and for the library itself.
An example:
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;
}
My first attempt was to create the external interface as an 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;
}
Pros:
Cons:
I hope you understand what I try to achieve and I'm looking forward to any suggestions.
Upvotes: 3
Views: 2427
Reputation: 224079
The simplest way might be to make setSecretParam()
private
and make the following a friend
of Algorithm
:
void setSecretParam(Algorithm& algorithm, double aParam)
{
void setSecretParam(double aParam);
}
Upvotes: 3
Reputation: 1310
The "usual suspect" to replace inheritance is Bridge pattern. You could define an hierarchy of "Imps" derived from abstract class AlgorithmImp and only expose appropriate algorithms in the library headers. Then an instance of algorithm can be created as
ConcreteAlgorithm ca1(SomeParam, new LibraryUserAlgorithm());
ConcreteAlgorithm ca2(SomeParam, new InternalAlgorithm());
Upvotes: 1