@Override
public Option getTrafficChartOption(String type, ReportType reportType, Integer deviceId, Integer direction) {
Integer device = deviceId + 1010000;
List<ChartData> data = chartDao.getTrafficChartData(reportType,device,direction);
String title = Titlehelper.getChartTitle(reportType);
String subtitle = Titlehelper.gettrafficSubText(reportType.getReportTime(),deviceId,direction);
Option option = new Option();
switch (type){
case "bar":
option = BarOption.BarOptionBuiler(title, subtitle, data);
break;
case "line":
option = LineOption.OptionBuilerhelp(title, subtitle, data);
break;
case "pie":
option = PieOption.PieOptionbuilder(title, subtitle, data);
break;
}
return option;
}
@Override
public Option getAmmeterChartOption(String type, ReportType reportType, Integer deviceId) {
List<ChartData> data = chartDao.getAmmeterDataChartData(reportType,deviceId);
String title = Titlehelper.getChartTitle(reportType);
String subtitle = Titlehelper.gettrafficSubText(reportType.getReportTime(),deviceId,1);
Option option = new Option();
switch (type){
case "bar":
option = BarOption.BarOptionBuiler(title, subtitle, data);
break;
case "line":
option = LineOption.OptionBuilerhelp(title, subtitle, data);
break;
case "pie":
option = PieOption.PieOptionbuilder(title, subtitle, data);
break;
}
return option;
}
代码结构非常相似,只是dao层取数据不一样,另外这个switch有没有改进空间,我知道使用eumn来枚举,没写以减少无关代码
8 回答

慕丝7291255
TA贡献1859条经验 获得超6个赞
String title = Titlehelper.getChartTitle(reportType);
String subtitle = Titlehelper.gettrafficSubText(reportType.getReportTime(),deviceId,1);
Option option = new Option();
switch (type){
case "bar":
option = BarOption.BarOptionBuiler(title, subtitle, data);
break;
case "line":
option = LineOption.OptionBuilerhelp(title, subtitle, data);
break;
case "pie":
option = PieOption.PieOptionbuilder(title, subtitle, data);
break;
}
return option;
这几行提取出来放在一个方法里调用不就行了

慕妹3146593
TA贡献1820条经验 获得超9个赞
@Override
public Option getTrafficChartOption(String type, ReportType reportType, Integer deviceId, Integer direction) {
return buildOption(Titlehelper.getChartTitle(reportType),
Titlehelper.getTrafficSubText(reportType.getReportTime(),deviceId,direction),
chartDao.getTrafficChartData(reportType,deviceId,direction));
}
@Override
public Option getAmmeterChartOption(String type, ReportType reportType, Integer deviceId) {
return buildOption(Titlehelper.getChartTitle(reportType),
Titlehelper.getTrafficSubText(reportType.getReportTime(),deviceId,1),
chartDao.getAmmeterDataChartData(reportType,deviceId));
}
private Option buildOption(String title, String subtitle, List<ChartData> data) {
Option option = new Option();
switch (type){
case "bar":
option = BarOption.BarOptionBuiler(title, subtitle, data);
break;
case "line":
option = LineOption.OptionBuilerhelp(title, subtitle, data);
break;
case "pie":
option = PieOption.PieOptionbuilder(title, subtitle, data);
break;
}
return option;
}

临摹微笑
TA贡献1982条经验 获得超2个赞
FlyObj.factory = function(type, option) {
if(type){
try{
return new FlyObj[type](option);
}catch(e){
console.log(e);
console.log( "无法找个----"+type+"----这个构造函数,请添加正确的构造函数——————(函数名错误)");
return false;
}
}
};
这样应该能缩短工厂函数
添加回答
举报
0/150
提交
取消