-1

Need to implement a requirement where the flow of code will be decided based on a lot of cases if implemented using switch case, OR a lot of if/else !

Sample code to be implemented:

if(flag='1')
   Invoke a new route

else If(flag='2')
   Invoke route2
.
.
.
.
else if(flag= 30)
  Invoke route 30

Is there a better approach to write such cases other than if/else statements or switch cases??

Something which is similar to internal implementation of workflow engines like jBPM but i actually cant include the workflow engine as it makes the application heavy!

Any suggestions appreciated!

mohammadReza Abiri
  • 1,759
  • 1
  • 9
  • 20
Myra
  • 25
  • 1
  • 1
  • 6
  • At the very least you may need to define "better". – NPE May 07 '19 at 18:42
  • It would be easier if you showed code examples of your alternatives, rather than trying to describe them in words. – Andy Turner May 07 '19 at 19:04
  • Sounds like a finite state machine would be a possibility. Or perhaps one or more maps with method references as values. – WJS May 07 '19 at 19:05
  • As it stands your question is both broad in scope and seeking opinions. That makes it off topic for Stack Overflow. Could you reword your question to be more focused, and avoid the giving the appearance of just looking for ideas and opinions? – skomisa May 07 '19 at 20:06
  • @skomisa Interesting since I have seen many very high rep folks give various solutions to problems which they each think is the best. I would call those opinions. – WJS May 07 '19 at 20:29
  • @WJS From [What types of questions should I avoid asking?](https://stackoverflow.com/help/dont-ask): _"avoid asking subjective questions where …you are asking an open-ended, hypothetical question"_. And regardless of that concern, the question is very broad in scope as it stands. – skomisa May 07 '19 at 20:40

1 Answers1

1

Here is what I was talking about above.


    import java.util.function.*;

    public class MethodCalls {

       public static void main(String[] args) {
          new MethodCalls().start();
       }

       public void start() {
          Map<Integer, Function<Integer, Integer>> callTable = new HashMap<>();
          callTable.put(1, a -> prod(a));
          callTable.put(2, a -> sub(a));
          Random r = new Random();
          r.ints(10, 1, 3).forEach(b -> System.out.println("The answer is "
                + callTable.get(b).apply(r.nextInt(20) + 20) + "\n"));
       }

       public int prod(int v) {
          System.out.println("Multiplying " + v + " by " + 40);
          return v * 40;
       }

       public int sub(int v) {
          System.out.println("Subtracting " + 30 + " from " + v);
          return v - 30;
       }
    }

This is just a toy but it demonstrates the possibilty of augmenting your switch and/or if/else statements with a call table. You may need several maps to handle different interface types or even declare your own functional interfaces to handle additional arguments. You might even make it more dynamic by incorporating reflection to cast functions during runtime.

WJS
  • 36,363
  • 4
  • 24
  • 39