search

Home  >  Q&A  >  body text

java - Code duplication and finding ways to improve it

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

The code structure is very similar, except that the dao layer fetches data differently. In addition, is there any room for improvement in this switch? I know that eumn is used to enumerate, but I did not write it to reduce irrelevant code

PHPzPHPz2749 days ago875

reply all(5)I'll reply

  • 怪我咯

    怪我咯2017-05-17 10:06:56

    Your code is to create the Option class, which is suitable for refactoring using factory methods.

    //方法参数尽量少于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);
        }
    }

    I won’t go into details about the benefits of using factory methods. You can read my blog for details

    reply
    0
  • 给我你的怀抱

    给我你的怀抱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;

    Just extract these lines and call them in a method

    reply
    0
  • 迷茫

    迷茫2017-05-17 10:06:56

    You can try the template design pattern, which extracts common algorithms into the parent class and leaves different parts to the subclass for rewriting. If there are fewer such places, it is easier to extract a public method.

    reply
    0
  • 高洛峰

    高洛峰2017-05-17 10:06:56

    What was said above is feasible, extract the public part and encapsulate it into a public method.

    reply
    0
  • ringa_lee

    ringa_lee2017-05-17 10:06:56

    The public extraction method mentioned above is simple and practical, but the answer on the first floor is too profound and needs to be thought about carefully

    reply
    0
  • Cancelreply