java中减少if,else判断

1.定义接口

public interface BusCurrentHandler {
    // 具体调用方法
    void getBusCurrents(BusCurrentVO vo);
    /**
     * 注入
     */
    void setTrendsFileService(BusTrendsFileService trendsFileService);
    void setHonorService(BusHonorService honorService);
    void setLikeService(LikeService likeService);
    void setBusTrendsDao(BusTrendsDao busTrendsDao);

}

2.接口实现类

@Service("honorsHandler")
public class HonorsHandler implements BusCurrentHandler {
    
    private LikeService likeService;

    private BusTrendsFileService trendsFileService;

    private BusHonorService honorService;

    @Override
    public void getBusCurrents(BusCurrentVO t) {
        t.setDel(AppConstants.TRUE);
        t.setCommentsCount(honorService.queryCommentCount(t.getId()));
        t.setLikeCount(likeService.getLikeCount(String.valueOf(t.getId()), AppConstants.HONOR));
        t.setFileUrls(trendsFileService.queryByTrendId(t.getId()));
        List<BusCurrentCommentVO> busHonorComments = this.honorService.queryAllComment(new BusHonorComment(t.getId()));
        busHonorComments.forEach(c -> {
            if (c.getCommenterId().equals(HttpAnalysisUtil.getUserId())) {
                c.setDel(AppConstants.TRUE);
            }
            c.setType(t.getType());
        });
        t.setComments(busHonorComments);
    }

    @Override
    public void setTrendsFileService(BusTrendsFileService trendsFileService) {
        this.trendsFileService =trendsFileService;
    }
    
    @Override
    public void setHonorService(BusHonorService honorService) {
        this.honorService = honorService;
    }

    @Override
    public void setLikeService(LikeService likeService) {
        this.likeService = likeService;

    }

    @Override
    public void setBusTrendsDao(BusTrendsDao busTrendsDao) {

    }
}

3.创建enum

public enum CommentEnum {
    TYPE1(new HonorsHandler() ,1),
    TYPE2(new TrendsHandler() ,2);
    private final BusCurrentHandler currentHandler;
    private final int type;

    CommentEnum(BusCurrentHandler currentHandler, int type) {
        this.currentHandler = currentHandler;
        this.type = type;
    }

    public int getType() {
        return type;
    }
    
    public void callService(BusCurrentVO vo, LikeService likeService, BusTrendsFileService trendsFileService, BusHonorService service, BusTrendsDao busTrendsDao){
        currentHandler.setLikeService(likeService);
        currentHandler.setHonorService(service);
        currentHandler.setTrendsFileService(trendsFileService);
        currentHandler.setBusTrendsDao(busTrendsDao);
        currentHandler.getBusCurrents(vo);
    }
}

4.service中调用

    @Override
    public List<BusCurrentVO> queryTrendsByCommenterId(Integer commenterId) {
        List<BusCurrentVO> busTrendsVOS = this.busTrendsDao.queryTrendsByCommenterId(commenterId);
        busTrendsVOS.forEach(t->{
            CommentEnum commentEnum = CommentEnum.valueOf("TYPE" + t.getType());
            commentEnum.callService(t,likeService,trendsFileService,honorService,busTrendsDao);
        });
        Collections.shuffle(busTrendsVOS);
        return busTrendsVOS;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值