31

Enum Tricks: Featured Enum Instead of Switch

 5 years ago
source link: https://www.tuicool.com/articles/hit/eyi2qme
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Problem and the Solution

Switch/case is the common control structure implemented in most imperative programming languages. Switch is considered more readable than a series of if/else.

Here is a simple example:

// Switch with int literal
switch(c) {
case1: one(); break;
case2: two(); break;
case3: three(); break;
default: thrownewUnsupportedOperationException(String.format("Operation %d is not supported", c));
}

Here is the list of the main problems in this code:

int
UnsupportedOperationException

The simplest fix can be done by using int   constants instead of literals. First, let's define constants:

private static int ONE = 1;

private static int TWO = 2;

private static int THREE = 3;

Now, the code will look like this:

switch(c) {
caseONE: one(); break;
caseTWO: two(); break;
caseTHREE: three(); break;
default: thrownewUnsupportedOperationException(String.format("Operation %d is not supported", c));
}

Obviously, in real life, the names of the constants must be self-descriptive. This snippet is more readable but all other disadvantages are still relevant. The next attempt to improve the initial code snippet uses enums introduced to Java language in version 5 in 2004. Let's define the following enum:

enumAction {ONE, TWO, THREE}

Now, the switch snippet will be changed slightly:

Action a = ...
switch(a) {
caseONE: one(); break;
caseTWO: two(); break;
caseTHREE: three(); break;
default: thrownewUnsupportedOperationException(String.format("Operation %s is not supported", a));
}

This code is a little bit better: it will produce compilation error if one of the elements is removed from enum Action. However, it will not cause a compilation error if an additional element is added to enum Action. Some IDEs or static code analysis tools may produce a warning in this case, but who is paying attention to warnings? Fortunately, an enum can declare an abstract method that has to be implemented by each element:

enum Action {

ONE { @Override public void action() { } },

TWO { @Override public void action() { } },

THREE { @Override public void action() { } },

public abstract void action()

}

Now, the switch statement can be replaced by single line:

Action a = ...a.action();

This solution does not have any of disadvantages enumerated above:

action()

Conclusion

Although switch/case structure is well known and widely used in various programming languages, its use may cause a lot of problems. The solution that uses Java enums and described above does not have these disadvantages. The next article from this series will show how to extend the functionality of an existing enum.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK