2

私はOOP設計が得意ではありませんが、潜在的な雇用主に自分の知識を示す必要があります。状況は次のとおりです。

と呼ばれるKey-Valueタイプのパラメーターを含むファイルがありますparameters.txt。私はmap <string, CommonParamValue>コンテナとして持っています。パラメータ付きのファイルを使用して、要素で埋める必要があります。キーは常にstd::stringパラメーターであり、は、、、および標準の関数呼び出しCommonParamValueで表すことができます。これを実装するために、は仮想メソッドを持つ基本クラスです。子がいます- 、、。基本クラスとすべての子には、内部データの文字列表現を返すメソッドがあります。値を設定します。intdoublestringCommonParamValueStringParamValueDoubleParamValueCurTimeParamValuevirtual string GetValue()virtual void SetValue(string)

map <string, CommonParamValue>問題は、ポリモーフィズムを使用して、実行時に適切なデータでコンテナをどのように満たすかです。今、私はそのような状況にあります:

parameters.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>

parameters.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内部のタイプを推測することにより、「タイプ」の追加の文字列を送り込むことを回避できます。正規表現は、、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 に答える