First of all, this is a design question rather than an issue:
I have an existing application written in C++.
The structure is as following:
class Step1
{
public:
Step1();
void process();
void setValue(int v); // is to change the value
private:
int value;
}
class Step2
{
public:
Step2();
void process();
void setValue(int v); // is to change the value
private:
int value;
}
void main()
{
Step1 step1();
Step2 step2();
step1.setValue(1);
step2.setValue(2);
step1.process();
step2.process();
}
I am now allowed to change the current architecture but it is required to configure all of the steps from outside. I am proposing to create an xml file holding the configuration settings:
<?xml version="1.0"?>
<Steps>
<Step id="step1">
<param method="setValue" variable="v" value="1" />
</Step>
<Step id="step2">
<param method="setValue" variable="v" value="2" />
</Step>
</Steps>
And then I will add a class to parse the xml file:
class XmlParser
{
public:
XmlParser();
int parse(string xmlFile, string paramValue);
}
then I think I will change the main class only:
void main()
{
Step1 step1();
Step2 step2();
XmlParser xmlParser();
step1.setValue(xmlParser.parse(,));
step2.setValue(xmlParser.parse(,));
step1.process();
step2.process();
}
I have more than 20 steps and there are about 10 methods to be configured in every step. Is it a good idea to change the architecture in this way that the XmlParser class will parse the xml file and pass the values to the set methods of the steps. Or is there a better way to do this?
I would like to know your opinions.
Thanks in advance.
I have already faced this problem and I have done something similar. I think it’s the most reasonable solution. For creation and setting of an instance of your processing classes (Steps, in your example) should be responsible your main subroutine/class.
One problem is parsing XML, you might change your current source code into the mess. To prevent that, you should create some class “Configuration”, that is responsible for parsing, error handling and default values of your settings.
Simple example of Configuration class interface:
Main subroutine is not filled with parsing stuff: