目前,我有這樣的代碼(我不喜歡它):如何將默認值設置爲方法的所有錯誤或空參數?
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;
}
...
}
我怎麼能提高呢?
我有一些關於引入一個將包含所有這些參數作爲字段的對象的想法,然後,也許可以應用生成器模式。但是仍然沒有清晰的眼光來實現這一點,我不確定這是值得做的。任何其他想法?