@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;
}
The code structure is very similar, except that the dao layer fetches data differently. In addition, is there any room for improvement in this switch? I know that eumn is used to enumerate, but I did not write it to reduce irrelevant code
Your code is to create the Option class, which is suitable for refactoring using factory methods.
I won’t go into details about the benefits of using factory methods. You can read my blog for details
Just extract these lines and call them in a method
You can try the template design pattern, which extracts common algorithms into the parent class and leaves different parts to the subclass for rewriting. If there are fewer such places, it is easier to extract a public method.
What was said above is feasible, extract the public part and encapsulate it into a public method.
The public extraction method mentioned above is simple and practical, but the answer on the first floor is too profound and needs to be thought about carefully