2

我不擅长 OOP 设计,但我需要向潜在雇主展示我的知识。情况如下:

我有一个带有键值类型参数的文件,名为parameters.txt. 我有一个map <string, CommonParamValue>作为容器。我需要使用带有参数的文件来填充元素。键始终是std::string参数,CommonParamValue可以用intdoublestring标准函数调用表示。为了实现这一点,它CommonParamValue是一个带有虚方法的基类。它有孩子 - StringParamValue, DoubleParamValue, CurTimeParamValue. 基类和每个子类都有一个virtual string GetValue()返回内部数据字符串表示的方法;virtual void SetValue(string)设置值。

问题是如何map <string, CommonParamValue>使用多态在运行时用适当的数据填充容器?现在我遇到了这样的情况:

参数.txt

*user_name=Jane
*order_number=1325
current_date=

地图填充程序

ifstream in(fileParamsPath);
    if (! in)
    {
        cout << "Cannot open file with parameters, program is terminating." << endl;
        cin.get();
        exit(-1);
    }
    string key = "", value = "";
    while(in)
    {
        getline(in, key, '=');
        getline(in, value, '\n');

        // put key and value into the container
        // right here we need to analyze the data type and choose appropriate container.
        // CommonParamValue *paramValue = new DoubleParamValue(); or
        // CommonParamValue *paramValue = new CurTimeParamValue(); or
        // CommonParamValue *paramValue = new StringParamValue();

        paramValue->SetValue(value);
        params.insert(make_pair(key, *paramValue)); // params is a map <string, CommonParamValue>
        delete paramValue;
    }
    in.close();

有一个想法是将值参数的类型保留在文件中parameters.txt并在填充时对其进行分析map <string, CommonParamValue>

参数.txt

*user_name=Jane
string

*order_number=1325
int

current_date=
function

并修改以map <string, CommonParamValue>这种方式填充的例程:

string key = "", value = "", type = "";
    while(in)
    {
        getline(in, key, '=');
        getline(in, value, '\n');
        getline(in, type, '\n');
        in.get(); // got the dividing empty string
        // put key and value into the container
        // right here we need to analyze the data type and choose appropriate container.
        if(type == "int")
        {
            CommonParamValue *paramValue = new IntParamValue();
        }
        else if(type == "function")
        {
            CommonParamValue *paramValue = new CurTimeParamValue();
        }
        else if(type == "string")
        {
            CommonParamValue *paramValue = new StringParamValue();
        }
        else
        {
            // error
            exit(1);
        }

        paramValue->SetValue(value);
        params.insert(make_pair(key, *paramValue)); // params is a map <string, CommonParamValue>
        delete paramValue;
    }

这是一个好决定还是坏决定?也许我的潜在雇主希望我以其他方式实施它,但我只有这个决定。对于初级 C++ 程序员,有没有更好的选择?

4

2 回答 2

2

最经典的适用设计是工厂方法设计

CommonParamValue* createParamValue( const std::string &value );

createParamValue方法负责创建适当的派生ParamValue并将其作为CommonParamValue.

您可以通过使用值字符串parameters.txt推断类型 inside来避免为“类型”注入额外的字符串。createParamValue对于,和,正则表达式听起来是最优雅的。Boost regex是一个示例库,可让您在 c++ 中执行此操作。但是当然,如​​果您正在寻找一个快速的解决方案,那么编写自己的 30 行决策树对于这些简单的语法来说应该不是问题。intdoubleCurTime

于 2012-06-19T08:43:12.687 回答
1

客户端代码中的 if 语句对我来说有点可疑。可以试试工厂课吗?

有一个基类 IParamValue

IntParamValue : public IParamValue { }

static IParamValue * Create(string selection);

IParamValue * IParamValue::Create(string selection)
{
    if (selection == "int") return new IntParamValue();
    ...
}

客户端代码:

IParamValue* param = IParamValue::Create ("something");
于 2012-06-19T08:50:18.560 回答