"if"声明与OO设计

7 java design-patterns enumeration if-statement

我有枚举说ErrorCodes

public enum ErrorCodes { 
       INVALID_LOGIN(100),
       INVALID_PASSWORD(101),
       SESSION_EXPIRED(102) ...;

       private int errorCode;

       private ErrorCodes(int error){
              this.errorCode = error;
       }    //setter and getter and other codes
}
Run Code Online (Sandbox Code Playgroud)

现在我用这个错误代码检查我的异常错误代码.如果这样做,我不想写,如果这样做的话.我怎么能解决这个问题(如果块写10+)

那种情况有什么设计模式吗?

谢谢

Spo*_*ike 7

您可以使用if语句或开关来执行此操作,或者只是以某种方式将有问题的逻辑实现到ErrorCode中.

在OO方式中,这一切都取决于您希望应用程序或系统对错误代码做出反应的方式.让我们说你只想输出一些对话框:

public doSomethingWithError() {

  ErrorCodes e = getError(); 
     // the source of error, or originator, returns the enum

  switch(e) {
    case ErrorCodes.INVALID_LOGIN:
      prompt('Invalid Login');
    case ErrorCodes.INVALID_PASSWORD:
      prompt('Invalid password');
    // and so on
  }

}
Run Code Online (Sandbox Code Playgroud)

我们可以改为创建一个ErrorHandler类来代替:

// We'll implement this using OO instead
public doSomethingWithError() {

  ErrorHandler e = getError(); 
    // the originator now returns an ErrorHandler object instead

  e.handleMessage();

}

// We will need the following abstract class:
public abstract class ErrorHandler {

   // Lets say we have a prompter class that prompts the message
   private Prompter prompter = new Prompter();

   public final void handleMessage() {
     String message = this.getMessage();
     prompter.prompt(message);
   } 

   // This needs to be implemented in subclasses because
   // handleMessage() method is using it.
   public abstract String getMessage();
}

// And you'll have the following implementations, e.g.
// for invalid logins:
public final class InvalidLoginHandler() {

  public final String getMessage() {
     return "Invalid login";
  }

}

// E.g. for invalid password:
public final class InvalidPasswordHandler() {
  public final String getMessage() {
    return "Invalid password";
  }
}
Run Code Online (Sandbox Code Playgroud)

前一种解决方案易于实现,但随着代码变大而变得难以维护.后一种解决方案更复杂,(也就是开放 - 封闭原则之后的模板方法模式),但是您可以在需要时添加更多方法(例如恢复资源或其他).您还可以使用策略模式实现此功能.ErrorHandler

您不会完全使用条件语句,但在后者中,条件被推送到发生错误的代码部分.这样,您不会在发起者和错误处理代码上对条件语句进行双重维护.

编辑:

迈克尔博格瓦特这个答案这个答案由oksayt如何,如果你想这样做,而不是实现Java的枚举的方法.


oks*_*ayt 3

正如 Spoike 所指出的,使用多态性来选择正确的错误处理方法是一种选择。这种方法基本上通过定义类层次结构将 10 多个 if 块推迟到 JVM 的虚拟方法查找。

但在寻求成熟的类层次结构之前,还要考虑使用enum方法。如果您在每种情况下计划执行的操作非常相似,则此选项非常有效。

例如,如果您想为每个返回不同的错误消息ErrorCode,您可以简单地执行以下操作:

// Note singular name for enum
public enum ErrorCode { 
   INVALID_LOGIN(100, "Your login is invalid"),
   INVALID_PASSWORD(101, "Your password is invalid"),
   SESSION_EXPIRED(102, "Your session has expired");

   private final int code;
   private final String 

   private ErrorCode(int code, String message){
          this.code = code;
          this.message = message;
   }

   public String getMessage() {
       return message;
   }
}
Run Code Online (Sandbox Code Playgroud)

那么你的错误处理代码就变成了:

ErrorCode errorCode = getErrorCode();
prompt(errorCode.getMessage());
Run Code Online (Sandbox Code Playgroud)

这种方法的一个缺点是,如果您想添加其他案例,则需要修改枚举本身,而使用类层次结构,您可以添加新案例而无需修改现有代码。