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文章来源:https://www.toymoban.com/news/detail-578113.html
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中调用文章来源地址https://www.toymoban.com/news/detail-578113.html
@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;
}
到了这里,关于减少if使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!