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.
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.
呵呵,是啊,反射的效率确实很低To ajoo(jet pig)
谢谢,足见高明:)
等我先应用试试
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.
you may try it in mutable way.
http://developer.ccidnet.com/pub/disp/Article?columnID=295&articleID=9586&pageNO=1
reflection is bad not only because of performance, type safety is the main drawback.
希望大家能就此展开讨论,也希望这样的讨论会对大家也有所帮助--我是获益非浅!
我是觉得ajoo(jet pig)的解决方法很有效,这问题也很典型,所以过早不想结贴,让大家都看看并参与讨论啊。
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!
不过,它的源码我一直没看过:(
谢谢,我马上去看。