Saturday, June 22, 2019

java - How to condense large switch statements

I have a large switch like following:



public int procList(int prov, ArrayList txValueList, Context context)
{
switch(prov)
{
case Foo.PROV_ONE:
return proc_one(txValueList, context);


case Foo.PROV_NOE:
return proc_noe(txValueList, context);

case Foo.PROV_BAR:
return proc_bar(txValueList, context);

case Foo.PROV_CABAR:
return proc_cabar(txValueList, context);


case Foo.PROV_FAR:
return proc_far(txValueList, context);

case Foo.PROV_TAR:
return proc_tar(txValueList, context);

case Foo.PROV_LBI:
return 408;

default:

return -1;
}
}


In c++ I can use std::map and use it in manner as following:



map[prov](txValueList, context); 



There is not pointer to function in Java. However, it uses abstract classes like it is in the answer. So, is there a best way to eliminate huge switch clauses in java?

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...