代码重复求改进方法

    @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来枚举,没写以减少无关代码

慕尼黑8549860
浏览 530回答 8
8回答

慕丝7291255

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; 这几行提取出来放在一个方法里调用不就行了

摇曳的蔷薇

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

慕妹3146593

@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; }

临摹微笑

FlyObj.factory = function(type, option) { if(type){ try{ return new FlyObj[type](option); }catch(e){ console.log(e); console.log( "无法找个----"+type+"----这个构造函数,请添加正确的构造函数——————(函数名错误)"); return false; } } }; 这样应该能缩短工厂函数

叮当猫咪

网站上大牛就是多

喵喵时光机

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

湖上湖

楼上说的提取公共方法简单实用,一楼的答案太高深,需要好好琢磨一下
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java