日期:2014-05-19  浏览次数:20652 次

两段代码,求重构方案,耦合度到最低!
代码1:
Java code

    public PageBase getQueryListTemplate(String templateName, String creater, String startDate, String endDate,
            String sortColumn, String dir, PageBase pageBase)
            throws ExceptionWithKey
    {

        PageBase pb = queryTemplateDao.getQueryListTemplate(templateName, creater, startDate, endDate,
                 sortColumn, dir, pageBase);

        // 查询 Time Level、Stat.Object两个字段的数据
        List<QueryTemplate> list = pb.getItems();
        for (QueryTemplate queryTemplate : list)
        {
            QueryTemplateContent content = queryTemplateContentDao.get(queryTemplate.getId());

            String moLevel = this.getMOLevelByID(content.getMoLevel(), false);
            content.setMoLevel(moLevel);

            String timeLevelName = this.getTimeLevelName(content.getTimeLevel());
            content.setTimeLevel(timeLevelName);
            queryTemplate.setQueryTemplateContent(content);
        }
        return pb;
    }



代码2:
Java code

public PageBase getQueryTemplate(long templateType, String searchKey,
            String sortColumn, String dir, PageBase pageBase)
            throws ExceptionWithKey
    {

        PageBase pb = queryTemplateDao.getQueryTemplate(templateType,
                searchKey, sortColumn, dir, pageBase);

        // 查询 Time Level、Stat.Object两个字段的数据
        List<QueryTemplate> list = pb.getItems();
        for (QueryTemplate queryTemplate : list)
        {
            QueryTemplateContent content = queryTemplateContentDao
                    .get(queryTemplate.getId());
            String moLevel = this.getMOLevelByID(content.getMoLevel(), false);
            content.setMoLevel(moLevel);

            String timeLevelName = this.getTimeLevelName(content.getTimeLevel());
            content.setTimeLevel(timeLevelName);
            queryTemplate.setQueryTemplateContent(content);
        }
        return pb;
    }



------解决方案--------------------
要降低神马跟神马的耦合?
看LZ的代码,既然是返回pb,那么遍历可以放在外面来做,既把List,for的部分写到一个方法,比如setContent方法,传入你的pb就可以了