@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來枚舉,沒寫以減少無關程式碼
怪我咯2017-05-17 10:06:56
你的程式碼是要建立Option類,適合使用工廠方法重構。
//方法参数尽量少于3个,3个以上时建议使用组合对象
class OptionParam {
private String type;
private ReportType reportType;
private Integer piceId;
private Integer direction;
//getter and setter
}
//Option抽象工厂,每个具体工厂都必须继承自抽象工厂
public abstract class AbstractOptionFactory {
@Autowired
ChartDao chartDao;
//希望具体工厂实现的方法,交于具体工厂实现
abstract List<ChartData> getData(OptionParam optionParam);
abstract String getSubtitle(OptionParam optionParam);
//公共逻辑代码,创建对象调用该方法
public Option create(OptionParam optionParam) {
assert optionParam != null;
assert optionParam.getReportType() != null;
String type = optionParam.getType();
List<ChartData> data = getData(optionParam);
String title = Titlehelper.getChartTitle(optionParam.getReportType());
String subtitle = getSubtitle(optionParam);
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;
}
}
@Component
class TrafficChartOptionFactory extends AbstractOptionFactory {
@Override
List<ChartData> getData(OptionParam optionParam) {
return chartDao.getTrafficChartData(optionParam.getReportType(),
optionParam.getpiceId(),
optionParam.getDirection());
}
@Override
String getSubtitle(OptionParam optionParam) {
return Titlehelper.gettrafficSubText(
optionParam.getReportType().getReportTime(),
optionParam.getpiceId(),
optionParam.getDirection());
}
}
@Component
class AmmeterChartOptionFactory extends AbstractOptionFactory {
@Override
List<ChartData> getData(OptionParam optionParam) {
return chartDao.getAmmeterDataChartData(optionParam.getReportType(), optionParam.getpiceId());
}
@Override
String getSubtitle(OptionParam optionParam) {
return Titlehelper.gettrafficSubText(optionParam.getReportType().getReportTime(),
optionParam.getpiceId(),
1);
}
}
使用工廠方法的好處就不多說了,具體可以閱讀我的部落格
给我你的怀抱2017-05-17 10:06:56
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;
這幾行提取出來放在一個方法裡呼叫不就行了