I have the necessity to execute some functions based on a particular CLI combination options (I use the Apache Commons CLI library).
myprogram -a -b ptionArgument -c
myprogram -a -c
myprogram -d optionArgument1 optionArgument2
To manage this situation, I have a very long list of "else if" based on a particular combination, all in my main function:
if (line.hasOption('a') && line.hasOption('b') && line.hasOption('c')){
method1();
method2();
}
else if (line.hasOption('a') && line.hasOption('c')){
method4();
}
else if (line.hasOption('d')){
method1();
method4();
}
....
Is there a better way to design this? (by using some design pattern for example).
I would come up with this creating
CombinationHandlerinterface withhandle()method and creating Map, where arguments is the key andCombinationHandler's are values. After that you can simply add different combinations ofCombinationHandler's to each argument combination and invoke them