为了账号安全,请及时绑定邮箱和手机立即绑定

代码重复求改进方法

代码重复求改进方法

慕尼黑8549860 2019-03-01 10:47:42
@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;

这几行提取出来放在一个方法里调用不就行了

查看完整回答
反对 回复 2019-03-01
?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

可以试试模板设计模式吧,将通用的算法提取到父类中,不同的地方交给子类重写。如果这样的地方少些,还是提取一个公共方法,比较省事。

查看完整回答
反对 回复 2019-03-01
?
慕妹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;        
}
查看完整回答
反对 回复 2019-03-01
?
临摹微笑

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;
      }
    }
  };
这样应该能缩短工厂函数
查看完整回答
反对 回复 2019-03-01
?
叮当猫咪

TA贡献1776条经验 获得超12个赞

网站上大牛就是多

查看完整回答
反对 回复 2019-03-01
?
喵喵时光机

TA贡献1846条经验 获得超7个赞

楼上说的可行,提取公共部分封装成一个公共方法。

查看完整回答
反对 回复 2019-03-01
?
湖上湖

TA贡献2003条经验 获得超2个赞

楼上说的提取公共方法简单实用,一楼的答案太高深,需要好好琢磨一下

查看完整回答
反对 回复 2019-03-01
  • 8 回答
  • 0 关注
  • 529 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号