私はOOP設計が得意ではありませんが、潜在的な雇用主に自分の知識を示す必要があります。状況は次のとおりです。
と呼ばれるKey-Valueタイプのパラメーターを含むファイルがありますparameters.txt
。私はmap <string, CommonParamValue>
コンテナとして持っています。パラメータ付きのファイルを使用して、要素で埋める必要があります。キーは常にstd::string
パラメーターであり、は、、、および標準の関数呼び出しCommonParamValue
で表すことができます。これを実装するために、は仮想メソッドを持つ基本クラスです。子がいます- 、、。基本クラスとすべての子には、内部データの文字列表現を返すメソッドがあります。値を設定します。int
double
string
CommonParamValue
StringParamValue
DoubleParamValue
CurTimeParamValue
virtual 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++プログラマーにとってより良いものはありますか?