RSS Feed for This PostCurrent Article

Java: Refactoring If-Then-Else using Enum

Download the code here

One thing good about object oriented language is that now we can reduce the conditional statements typically found in procedural language, which in turn can make our code more maintainable.

In Java, normally if we want to refactor a long conditional statements, we use polymorphism, which results in a lot of classes.

Another way to do it is to use Enum.

As an example, consider the following code.

   1: package twit88.procedural;
   2:  
   3: import org.apache.commons.lang.StringUtils;
   4:  
   5: public class IfThenElse {
   6:  
   7:  
   8:     public void invoke(String operationName) {
   9:         if (StringUtils.equals(operationName, "operation1")) {
  10:             operation1();
  11:         } else if (StringUtils.equals(operationName, "operation2")) {
  12:             operation2();
  13:         } else if (StringUtils.equals(operationName, "operation3")) {
  14:             operation3();
  15:         }
  16:  
  17:     }
  18:  
  19:     public void operation1() {
  20:         System.out.println("operation 1");
  21:     }
  22:  
  23:     public void operation2() {
  24:         System.out.println("operation 2");
  25:     }
  26:  
  27:     public void operation3() {
  28:         System.out.println("operation 3");
  29:     }
  30:  
  31:  
  32:     public static void main(String[] args) {
  33:         IfThenElse obj = new IfThenElse();
  34:         obj.invoke("operation2");
  35:     }
  36:  
  37: }

As you can see, there if then else code is ugly and make the code difficult to maintain.

Here is a new version using Enum

   1: package twit88.refactor;
   2:  
   3: import java.util.HashMap;
   4: import java.util.Map;
   5:  
   6:  
   7: public class NewIfThenElse {
   8:  
   9:     private interface IOperation {
  10:         void apply(String name);
  11:     }
  12:  
  13:     private enum Operation implements IOperation {
  14:  
  15:         OPERATION_1("operation1") {
  16:             public void apply(String name) {
  17:                 System.out.println("operation 1");
  18:             }
  19:         },
  20:         OPERATION_2("operation2") {
  21:             public void apply(String name) {
  22:                 System.out.println("operation 2");
  23:             }
  24:         },
  25:         OPERATION_3("operation3") {
  26:             public void apply(String name) {
  27:                 System.out.println("operation 3");
  28:             }
  29:         };
  30:  
  31:  
  32:         private static Map<String, Operation> requestLookup;
  33:  
  34:         static {
  35:             requestLookup = new HashMap<String, Operation>(3);
  36:             requestLookup.put(OPERATION_1.getName(), OPERATION_1);
  37:             requestLookup.put(OPERATION_2.getName(), OPERATION_2);
  38:             requestLookup.put(OPERATION_3.getName(), OPERATION_3);
  39:         }
  40:  
  41:         private final String name;
  42:  
  43:         private Operation(String name) {
  44:             this.name = name;
  45:         }
  46:  
  47:         public String getName() {
  48:             return name;
  49:         }
  50:  
  51:         @Override
  52:         public String toString() {
  53:             return "Operation{" + "name='" + name + '\'' + '}';
  54:         }
  55:  
  56:         public static Operation getOperationByName(String name) {
  57:             return requestLookup.get(name);
  58:         }
  59:     }
  60:  
  61:     public void invoke(String operationName) {
  62:         Operation operation = Operation.getOperationByName(operationName);
  63:         if (operation != null) {
  64:             operation.apply(operationName);
  65:         }
  66:     }
  67:  
  68:     public static void main(String[] args) {
  69:         NewIfThenElse obj = new NewIfThenElse();
  70:         obj.invoke("operation1");
  71:     }
  72: }


Trackback URL


Sorry, comments for this entry are closed at this time.