Java – Code-Duplizierung und Wege finden, ihn zu verbessern
PHPz
PHPz 2017-05-17 10:04:56
0
5
869
    @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

PHPz
PHPz

学习是最好的投资!

Antworte allen(5)
刘奇

你的代码是要创建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);
    }
}

使用工厂方法的好处就不多说了,具体可以阅读我的博客

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

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

迷茫

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

小葫芦

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

左手右手慢动作

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

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage