如何重构重复切换以过滤不同的调用

时间:2016-09-08 17:58:29

标签: java switch-statement repository refactoring

我的服务上有几个方法可以调用不同的Spring Data自动存储库方法。我该如何重构这段代码?它闻到无处不在!

public List<SupportMessages> findByProduct(Product product, Status status) {
  Date date = new Date();
  switch(status) {
    case EXPIRED:
      return supportMessageRepository.findByProductAndDateAfter(product, date);
    case VALID:
      return supportMessageRepository.findByProductAndDateBefore(product, date);
    case ALL:
      return supportMessageRepository.findByProduct(product);  
}


public List<SupportMessages> findByRegion(Region region, Status status) {
  Date date = new Date();
  switch(status) {
    case EXPIRED:
      return supportMessageRepository.findByRegionAndDateAfter(region, date);
    case VALID:
      return supportMessageRepository.findByRegionAndDateBefore(region, date);
    case ALL:
      return supportMessageRepository.findByRegion(region);  
}

public List<SupportMessages> findByClient(Client client, Status status) {
  Date date = new Date();
  switch(status) {
    case EXPIRED:
      return supportMessageRepository.findByClientAndDateAfter(client, date);
    case VALID:
      return supportMessageRepository.findByClientAndDateBefore(client, date);
    case ALL:
      return supportMessageRepository.findByClient(client);  
}

0 个答案:

没有答案