例えば、データクラスの番号によって呼び出す処理を変える場合で、パターンを使わずそのクラス内だけで簡潔に書いて終わらせたい場合、
[A] public void execute(List<Item> itemList){ for(Item item:itemList){ int itemNo = item.getNo(); //itemNoごとに呼ぶメソッドを分けたい switch(itemNo){ case 1: executeItem1(itemNo); ..... } } } [B] public void execute(List<Item> itemList){ for(Item item:itemList){ int itemNo = item.getNo(); //itemNoごとに呼ぶメソッドを分けたい try{ getClass().getMethod("executeItem"+itemNo,new Class[]{Item.class}).invoke(this,new Object[]{item}); }catch(Exception e){ throw new RuntimeException(e); } } }
switch文がめんどくさいのでBを採用する場合があるんですが、気持ち悪いと言う人もいます。
ActionScriptなんかだとthis["executeItem"+i](item)とかやってもあんまり拒否感はない様なんですが、やっぱりリフレクションAPIは業務ロジック向きではないですかね。