I wonder what is the best practice to configure policies in policy based design. The interface of the policy is defined by its host class. Nevertheless, it is not defined how this interface needs to be implemented. Therefore, policies might need different configuration parameters. But I only want to feed one configuration (object) into the host class. The following code is a draft what I have done so far, but I am not so happy with it because it does not guarantee that there are no naming conflicts and it does not encapsulate configuration members of different policies.
class Policy1 {
template<class Config>
Policy1(Config cp1) {
/* Use policy1 config members */
}
};
class Policy2 {
template<class Config>
Policy2(Config cp2) {
/* Use policy2 config members */
}
};
template <class P1, class P2>
class Host {
template<class Config>
Host(Config c) : p1(P1(c)), p2(P2(c)) {...}
P1 p1;
P2 p2;
};
struct Config {
int configParam1; /* Used by policy 1 */
int configParam2; /* Used by policy 2 but might also
be called configParam1
*/
}
int main() {
Host<Policy1, Policy2> h(Config());
return 0;
}
How can I prevent name conflicts in the configuration objects? Is there some mechanism similar to namespaces? Or does somehow has a better idea to configure my policies?