How to condense large switch statements
我有一个大开关,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public int procList(int prov, ArrayList<TXValue> 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; } } |
在C++中,我可以使用EDCOX1×0,并以如下方式使用它:
1 | map[prov](txValueList, context); |
Java中没有指向函数的指针。但是,它使用抽象类,就像它在答案中一样。那么,有没有一种最好的方法来消除Java中的EDCOX1 1条子句呢?
您要查找的是枚举。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | public enum Prov { PROV_ONE(Foo.PROV_ONE) { @Override public int provMethod(List<TXValue> txValueList, Context context) { return proc_one(txValueList, context); } }, PROV_NOE(Foo.PROV_NOE) { @Override public int provMethod(List<TXValue> txValueList, Context context) { return proc_noe(txValueList, context); } }, PROV_BAR(Foo.PROV_BAR) { @Override public int provMethod(List<TXValue> txValueList, Context context) { return proc_bar(txValueList, context); } }, PROV_CABAR(Foo.PROV_CABAR) { @Override public int provMethod(List<TXValue> txValueList, Context context) { return proc_cabar(txValueList, context); } }, PROV_FAR(Foo.PROV_FAR) { @Override public int provMethod(List<TXValue> txValueList, Context context) { return proc_far(txValueList, context); } }, PROV_TAR(Foo.PROV_TAR) { @Override public int provMethod(List<TXValue> txValueList, Context context) { return proc_tar(txValueList, context); } }, PROV_LBI(Foo.PROV_LBI) { @Override public int provMethod(List<TXValue> txValueList, Context context) { return 408; } }, UNKNOWN(Integer.MIN_VALUE) { // make sure this is not used in other IDs //decide if you actually need this @Override public int provMethod(List<TXValue> txValueList, Context context) { return -1; } }; private int id; private Prov(int id) { this.id = id; } public abstract int provMethod(List<TXValue> txValueList, Context context); public static Prov getById(int id) { for(Prov prov : Prov.values()) { if(id == prov.id) { return prov; } } //return null; //throw new IllegalArgumentException("Specified id was not found."); return Prov.UNKNOWN; } } |
然后你可以做
1 2 3 4 | public int procList(int prov, ArrayList<TXValue> txValueList, Context context) { return Prov.getById(prov).provMethod(txValueList, context); } |
回答OP的问题-
1 2 3 4 5 6 7 8 9 10 11 12 13 | Map<Integer, BiFunction<ArrayList<TXValue>,Context, Integer>> map = new HashMap<>(); { map.put(Foo.PROV_ONE, this::proc_one); // etc .... map.put(Foo.PROV_LBI, (x,y)->408 ); } public int procList(int prov, ArrayList<TXValue> txValueList, Context context) { if( ! map.contains(prov)) return -1; return map.get(prov).apply(txValueList, context); } |