ues jndi to replace reflection?
jndi may be even heavy than reflection.just a simple java.util.Map should be able to do this.

解决方案 »

  1.   

    return Class.forName(name).newInstance();
    事先不须要知道类是否存在。
    X Factory.create(name)中return (X)context.lookup(name);
    事先要绑定要Lookup的类,即使存在而没有绑定的类是LOOKUP不了的。---------------
    [email protected]
      

  2.   

    how do you call the create() method anyway?
    if you say Factory.create("A"), what prevents you from saying new A()?
    if you read the class name from file, then there's no way around. you'll have to call newInstance() for at least once.
      

  3.   

    忘记提一点了,在每个实际产品类的static域中,都会将这个类的实例注册到JNDI树或者HashMap中了,这样,就不会找不到了确实,用JNDI是比HashMap麻烦一些对于反射,1. 如果我加载了一个确实存在的类,但是不是符合要求的X的实现类,那么启不是要出错?2. 在上面的例子中X Factory.create(name)里应该是(X)Class.forName(name).newInstance();而不是Class.forName(name).newInstance()吧