我认为你把事情复杂化了,最终会创建太多的类。
我认为这个问题与其说是构建字符串,不如说是你如何为.net 调用者提供一个漂亮的接口。仅仅因为有很多可能的参数并不意味着你需要为它们创建很多类,至少如果每个参数中的可能变化不是很复杂,即如果值存在或不存在,并且有可选单位。然后你让自己陷入需要知道命令部分需要的顺序等的混乱中。
可能我会为命令的可能参数创建一个类,让调用者设置他们喜欢的任何参数,并让该类负责在一个大的(丑陋的?)字符串连接中生成字符串。
例如,我会从这样的东西开始,然后我会添加一些方法,根据命令不同部分的相似性,重构一些字符串连接是有意义的。
class CommandArgs
{
private double? _Position_x = null;
private double? _Position_y = null;
private String _Position_units = null;
private double? _Width = null;
private String _Width_units = null;
private double? _Height = null;
private String _Height_units = null;
// maybe there's a better tuple-like type for this.
public double[] Position
{
set
{
if (value.length != 2) throw new ArgumentException("argh!");
_Position_x = value[0];
_Position_y = value[1];
}
}
public string Position_Units
{
set
{
_Position_Units = value;
}
}
public double Width
set
{
_Width = value;
}
}
public double Height
set
{
_Height = value;
}
}
public string Width_Units
set
{
_Width = value;
}
}
public string Height_Units
set
{
_Height = value;
}
}
// ....
public override string ToString()
{
return
( _Position_x != null ? string.Format(" Position ({0},{1})",_Position_x, _Position_y ) : "" )
+ ( _Height != null ? string.Format(" Height {0}")
+ ( _Height_Units != null ? string.Format(" Units {0}", _Height_Units) : "" )
+ ( _Width != null ? string.Format(" Width {0}")
+ ( _Width_Units != null ? string.Format(" Units {0}", _Width_Units) : "" )
// ...
;
}
}
如果您愿意,您可能希望创建方法而不是属性,因此您可以同时设置值和单位。
我可能会立即使用以下方法重构它:
private string FormatUnits(string units)
{
return units == null ? "" : string.Format(" Units {0}", units);
}
private string FormatSingleValueArgument(string argName, object argValue, string units)
{
if (argValue == null) return "";
return string.Format(" {0} {1}", argName, argValue) + FormatUnits(units);
}
使 ToString() 看起来像这样:
return
( _Position_x != null ? string.Format(" Position ({0},{1})",_Position_x, _Position_y ) : "" )
+ FormatSingleValueArgument("Height", _Height, _Height_Units)
+ FormatSingleValueArgument("Width", _Width, _Width_Units)
// ...
;
如果有几个类似位置的参数,那么也许可以为类似位置的参数做一个类似的方法。