简体   繁体   English

在工厂设计模式中避免 if-else、switch-case 的方法

[英]Ways to Avoid if-else, switch-case in Factory design pattern

I am designing a validation module.我正在设计一个验证模块。 It has 100 error codes(ie errcd_01, errcd_02,..,errcd_100) to be validated.它有 100 个要验证的错误代码(即 errcd_01、errcd_02、..、errcd_100)。 In input I am getting a specific error code(ie errcd_01) out of above 100. Module should perform validation for that specific error code.在输入中,我得到了一个超过 100 个的特定错误代码(即 errcd_01)。模块应该对该特定错误代码执行验证。

I am using factory pattern.我正在使用工厂模式。

/* Interface */
public interface validateErrCd {
   void check_errcd();
}

/* Concrete classes implementing the same interface */
public class validateErrCd_01 implements validateErrCd {

   @Override
   public void check_errcd() {
      //business logic related to errcd_01
   }
}

public class validateErrCd_02 implements validateErrCd {

   @Override
   public void check_errcd() {
      //business logic related to errcd_02
   }
}
.
.
.
public class validateErrCd_100 implements validateErrCd {

   @Override
   public void check_errcd() {
      //business logic related to errcd_100
   }
}

/* Factory */
public class ErrorValidationFactory {
    
   //use check_errcd method to get object of type shape 
   public validateErrCd getValidation(String errorCode){
      if(errorCode == null){
         return null;
      }     
      if(errorCode.equalsIgnoreCase("errcd_01")){
         return new validateErrCd_01();
         
      } else if(errorCode.equalsIgnoreCase("errcd_02")){
         return new validateErrCd_02();
         
      } ..
       .......
      else if(errorCode.equalsIgnoreCase("errcd_100")){
         return new validateErrCd_100();
      }
      else {
           return null;
      }
   }
}

/* I am using the Factory to get object of concrete class by passing an specific error code to be validated (i.e. "errcd_01"). */
public class FactoryPatternDemo {

   public static void main(String[] args) {
      ErrorValidationFactory errorFactory = new ErrorValidationFactory();

      //get an object of validateErrCd_01 and call its check_errcd method.
      validateErrCd errcd01 = errorFactory.getValidation("errcd_01");

      //call check_errcd method of validateErrCd_01
      errcd01.check_errcd();
   }
} 

Now due to multiple if/else inside Factory class ErrorValidationFactory, I am getting couple of CI/CD errors while performing mvn clean install.现在由于 Factory class ErrorValidationFactory 中有多个 if/else,我在执行 mvn clean install 时遇到了几个 CI/CD 错误。 eg [MethodLength] - checkstyle, Rule:CyclomaticComplexity - PMD.例如 [MethodLength] - checkstyle,Rule:CyclomaticComplexity - PMD。

So is there a way I can replace if/else, switch case kind of decision making inside factory which does not trigger above CI/CD errors in Java?那么有没有一种方法可以替换工厂内部的 if/else、switch case 类型的决策,它不会触发 Java 中的上述 CI/CD 错误?

Note: If possible I would like to avoid reflection注意:如果可能的话,我想避免反思

You could use a Map :您可以使用Map

public class ErrorValidationFactory {
    private Map<String,Supplier<validateErrCd>> creators=new HashMap<>();
    public ErrorValidationFactory(){
        creators.put("errcd_100",validateErrCd_100::new);
        //Same for others
    }
   //use check_errcd method to get object of type shape 
   public validateErrCd getValidation(String errorCode){
        if(errorCode == null){
           return null;
        }
        return creators.getOrDefault(errorCode,()->null);
   }
}

Supplier is a functional interface that contains a method returning an object. Supplier是一个功能接口,它包含一个返回 object 的方法。 SomeClass::new or ()->new SomeClass() means that the constructor of the class will be used for that. SomeClass::new()->new SomeClass()表示将使用 class 的构造函数。

This allows to to create the instances later.这允许稍后创建实例。

If you want to create the Map only once, you can make it static and populate it in a static initializer.如果您只想创建一次Map ,您可以将其static并将其填充到 static 初始化程序中。

However, if you really want to dynamically get the constructors, you would need to use reflection.但是,如果您真的想动态获取构造函数,则需要使用反射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM