Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何重构重复开关以过滤不同的调用_Java_Switch Statement_Repository_Refactoring - Fatal编程技术网

Java 如何重构重复开关以过滤不同的调用

Java 如何重构重复开关以过滤不同的调用,java,switch-statement,repository,refactoring,Java,Switch Statement,Repository,Refactoring,我的服务中有几个方法调用不同的Spring数据自动存储库方法。如何重构此代码?到处都是味道 public List<SupportMessages> findByProduct(Product product, Status status) { Date date = new Date(); switch(status) { case EXPIRED: return supportMessageRepository.findByProductAndDate

我的服务中有几个方法调用不同的Spring数据自动存储库方法。如何重构此代码?到处都是味道

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);  
}
公共列表findByProduct(产品、状态){
日期=新日期();
开关(状态){
过期个案:
返回supportMessageRepository.findByProductAndDateAfter(产品,日期);
有效案例:
返回supportMessageRepository.findByProductAndDateBefore(产品,日期);
所有案例:
返回supportMessageRepository.findByProduct(产品);
}
公共列表findByRegion(区域、状态){
日期=新日期();
开关(状态){
过期个案:
返回supportMessageRepository.findByRegionAndDateAfter(区域,日期);
有效案例:
返回supportMessageRepository.findByRegionAndDateBefore(地区,日期);
所有案例:
返回supportMessageRepository.findByRegion(区域);
}
公共列表findByClient(客户端,状态){
日期=新日期();
开关(状态){
过期个案:
返回supportMessageRepository.findByClientAndDateAfter(客户端,日期);
有效案例:
返回supportMessageRepository.findByClientAndDateBefore(客户端,日期);
所有案例:
返回supportMessageRepository.findByClient(客户端);
}

开关中调用或正在调用的方法似乎都有不同的解决方法,在这种情况下,这差不多就是最好的解决方法了。另一种模式是在
状态
枚举本身中实现这些
findX
方法。您能否澄清显示的类和方法中您控制的是哪一个(即,您可以重构或重命名)?@PNS我可以重构所有类,甚至是存储库方法。存储库是最大的问题吗?supportMessageRepository是单例的吗?你能重命名它的方法吗?@PNS是的,我使用Spring,并用
@Autowired
注释。据我所知,在使用
@Autowired
时,Spring会注入单例对象。