will this work?
public interface ActionManager
{
    public Action getAction(ActionId id)
       throws NoSuchElementException;
    public ActionManager addAction(ActionId id, Action act);
}Then, in some action registration function, do
ActionManager register(ActionManager man)
{
    return man.addAction(ActionIdFactory.buildActionId("ForumList", ForumListAction.instance())
       .addAction(ActionIdFactory.buildActionId("ForumMessageList", ForumMessageAction.instance())
       ......
      ;
}it does not make any sense to limit it to 25 lines if you have 1000 actions to register.

解决方案 »

  1.   

    To eyeieye(魔之眼)
    呵呵,是啊,反射的效率确实很低To ajoo(jet pig)
    谢谢,足见高明:)
    等我先应用试试
      

  2.   

    first of all, register is not type safe. you can easily end up with delicate code.
    if you have code like Class.forName("A").newInstance();
    you are requiring 
    1. A is a class (you cannot make it an inner class, make it private or change the name later)
    2. A has to have a public constructor with no parameter.The problem here is, even if you change A class, the compiler won't complain! the Java type system does not protect you from stupid mistakes. You may find out this problem a year after production!secondly, reflection is inefficient.
      

  3.   

    my code assumes an immutable hashtable. but you don't have the source code.
    you may try it in mutable way.
      

  4.   

    是否可以用运行时的子类识别技术?
    http://developer.ccidnet.com/pub/disp/Article?columnID=295&articleID=9586&pageNO=1
      

  5.   

    don't use reflection, RTTI unless necessary. 
    reflection is bad not only because of performance, type safety is the main drawback.
      

  6.   

    呵呵,我加分了,ajoo(jet pig)兄100分当之无愧,其它捧场的兄弟也会给分--唉,为什么不让我放1000分啊:(
    希望大家能就此展开讨论,也希望这样的讨论会对大家也有所帮助--我是获益非浅!
      

  7.   

    唉,大家都不捧场啊:(
    我是觉得ajoo(jet pig)的解决方法很有效,这问题也很典型,所以过早不想结贴,让大家都看看并参与讨论啊。
      

  8.   

    one senario that you may want to use reflection is:
    you are concerned about the cost of deploying new class and functionality.  
    With reflection, you simply configure your new class name in some configuration xml file and your application can automatically use that class file. No code change and recompilation is necessary.The drawback is, you don't have the Java type system to protect you. have to be very careful!
      

  9.   

    这个跟Struts的思路差不多吧,可以参考一下他的实现。好像是用Map,初始化时用Reflection读配置文件填Map,然后匹配时直接用Map就可以了。
      

  10.   

    呵呵,是啊,主要是因为Structs还不很稳定,功能也有所欠缺,所以我想有自己的FrameWorks :)
    不过,它的源码我一直没看过:(
    谢谢,我马上去看。