It depend of you're XML file structure. If I could access A/B/C/... seperately my c++/boost code would look like this.
Refactor out all the A related stuff in class FunctionalityA
, B related stuff in class FunctionalityB, ...
FunctionalityProvider class is the class where you configure the functionality of your system.
TheOneAndOnlyMethod asks the provider for all the functionality and iterates over them.
class XmlFunctionality
{
public:
virtual ~XmlFunctionality(){
}
virtual void loadFromConfig(XmlBasedConfig) = 0;
virtual bool isEnabled() const = 0;
virtual void execute() = 0;
protected:
XmlFunctionality(){
};
}
class FunctionalityA : public XmlFunctionality
{
public:
void loadFromConfig(XmlBasedConfig){
// load A information from xml
}
bool isEnabled() const{
return globalA.length()>0; // is A a global !?
}
void execute(){
// do you're a related stuff
}
}
class FunctionalityB : public XmlFunctionality
{
public:
void loadFromConfig(XmlBasedConfig){
// load B information from xml
}
bool isEnabled() const{
// when is b enabled ...
}
void execute(){
// do you're b related stuff
}
}
// Map your question to the functions -
class FunctionalityProvider
{
Functionality functionalityList;
public:
typedef std::vector<XmlFunctionality*> Functionality;
FunctionalityProvider() : functionalityList() {
functionalityList.push_back( new FunctionalityA);
functionalityList.push_back( new FunctionalityB);
functionalityList.push_back( ... );
}
~FunctionalityProvider {
for_each( functionality.begin(), functionality.end(), delete_ptr() );
}
Functionality functionality() const {
return functionalityList;
}
}
void TheOneAndOnlyMethod(XmlBasedConfig config, const FunctionalityProvider& provider) {
const FunctionalityProvider::Functionality functionality = provider.functionality();
for_each(
functionality.begin(),
functionality.end(),
bind(&XmlFunctionality::loadFromConfig, _1, config)); // or some other way of parsing the file
for_each(
functionality.begin(),
functionality.end(),
if_then( bind(&XmlFunctionality::isEnabled, _1), bind(&XmlFunctionality::execute, _1)));
}
If I couldn't access A/B/C seperately I let a parser returing a list of Functionality based on the content of the XML file.