目前,我有这样的代码(我不喜欢它):如何将默认值设置为方法的所有错误或空参数?
private RenderedImage getChartImage (GanttChartModel model, String title,
Integer width, Integer height,
String xAxisLabel, String yAxisLabel,
Boolean showLegend) {
if (title == null) {
title = "";
}
if (xAxisLabel == null) {
xAxisLabel = "";
}
if (yAxisLabel == null) {
yAxisLabel = "";
}
if (showLegend == null) {
showLegend = true;
}
if (width == null) {
width = DEFAULT_WIDTH;
}
if (height == null) {
height = DEFAULT_HEIGHT;
}
...
}
我怎么能提高呢?
我有一些关于引入一个将包含所有这些参数作为字段的对象的想法,然后,也许可以应用生成器模式。但是仍然没有清晰的眼光来实现这一点,我不确定这是值得做的。任何其他想法?