How do you refactor a switch statement in Java?

How do you refactor a switch statement in Java?

Seven Ways to Refactor Java switch Statements

  1. Implementing the Strategy Pattern via Java Enum. Application Name: SwitchToStrategyEnum.
  2. Implementing the Command Pattern.
  3. Using the Java 8+ Supplier.
  4. Defining a Custom Functional Interface.
  5. Relying on Abstract Factory.
  6. Implementing a State Pattern.

When switch statements have large sets of case clauses it is usually an attempt to map two sets of data?

When switch statements have large sets of case clauses, it is usually an attempt to map two sets of data. A real map structure would be more readable and maintainable, and should be used instead.

How do you replace a switch statement?

Switch statements can often be replaced by a good OO design….This can be implemented in a number of ways, which have been mentionned in other answers to this question, such as:

  1. A map of values -> functions.
  2. Polymorphism. (the sub-type of an object will decide how it handles a specific process).
  3. First class functions.

What can we use instead of switch case?

Some alternatives to switch statements can be:

  • A series of if-else conditionals that examine the target one value at a time.
  • A lookup table, which contains, as keys, the case values and, as values, the part under the case statement.

Is switch a replacement for if?

Check the Testing Expression: An if-then-else statement can test expressions based on ranges of values or conditions, whereas a switch statement tests expressions based only on a single integer, enumerated value, or String object. Prefer switch if the number of cases are more than 5 otherwise, you may use if-else too.

Is it possible to refactor a switch statement?

Normally I refactor switch statements through polymorphism. You can still inject the desired behavior with an IoC. I’m with Sergio there, a switch statement can usually be replaced by the careful application of polymorphism.

How to reduce cyclomatic complexity in a switch statement?

This article describes refactoring a switch statement in order to reduce Cyclomatic complexity. Many developers would have scratched their heads in order to keep their Cyclomatic complexity under 10. In some cases, it is really not possible at first site. In this article, I have provided a way to do that. Take the example of a Device State machine.

How are switch statements used in object oriented code?

You have a complex switch operator or sequence of if statements. Relatively rare use of switch and case operators is one of the hallmarks of object-oriented code. Often code for a single switch can be scattered in different places in the program. When a new condition is added, you have to find all the switch code and modify it.

Can a switch statement be replaced in a method?

I’m with Sergio there, a switch statement can usually be replaced by the careful application of polymorphism. If you refactor such that the switch statement is the only thing in a method, and each entry in the switch statement calls only another method, then the state for each case and each method can usually be grouped into its own subclass.