亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

代碼重復求改進方法

代碼重復求改進方法

慕尼黑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 關注
  • 527 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號