Heim > Fragen und Antworten > Hauptteil
@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;
}
Die Codestruktur ist sehr ähnlich, außer dass die Dao-Schicht Daten anders abruft. Gibt es außerdem Raum für Verbesserungen bei diesem Schalter? Ich weiß, dass eumn zum Aufzählen verwendet wird, aber ich habe es nicht geschrieben, um irrelevanten Code zu reduzieren
怪我咯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;
这几行提取出来放在一个方法里调用不就行了