配置用 C++ 编写的现有应用程序
首先,这是一个设计问题而不是问题:
我有一个用 C++ 编写的现有应用程序。
结构如下:
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();
}
我现在可以更改当前的架构,但需要从外部配置所有步骤。我建议创建一个包含配置设置的 xml 文件:
<?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>
然后我将添加一个类来解析 xml 文件:
class XmlParser { 民众: XmlParser(); int parse(字符串 xmlFile, 字符串 paramValue); 那么
我想我只会改变主类:
void main()
{
Step1 step1();
Step2 step2();
XmlParser xmlParser();
step1.setValue(xmlParser.parse(,));
step2.setValue(xmlParser.parse(,));
step1.process();
step2.process();
}
我有20多个步骤,每个步骤大约有10个方法需要配置。以这种方式更改体系结构是一个好主意吗?XmlParser 类将解析 xml 文件并将值传递给步骤的 set 方法。或者有更好的方法来做到这一点吗?
我想知道你的意见。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经遇到过这个问题并且我也做过类似的事情。我认为这是最合理的解决方案。对于处理类实例的创建和设置(在您的示例中为步骤)应该由您的主子例程/类负责。
其中一个问题是解析 XML,您可能会将当前的源代码弄得一团糟。为了防止这种情况,您应该创建一些“Configuration”类,它负责解析、错误处理和设置的默认值。
配置类接口的简单示例:
主子例程没有填充解析内容:
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:
使用某种依赖注入。
如果您决定将
XmlParser
传递给 Step 对象,那么您的示例将如下所示:这很好,因为您可以将模拟对象传递给单元测试中的步骤对象。
Use some kind of dependency injection.
If you decide to pass the
XmlParser
to Step objects, then your example will look like this :This is good, because you can pass mock objects to the step objects in your unit tests.