其中StorefrontMemoryDatabasePlugIn这个l类中有这么一段代码:
IStorefrontServiceFactory factory = null;try {
  factory = StorefrontServiceFactory.getInstance();
} catch (Exception ex) {
  throw new ServletException(ex.getMessage());
}IStorefrontServiceFactory是一个接口;
public interface IStorefrontServiceFactory {
public IStorefrontService createService()
throws ClassNotFoundException, IllegalAccessException, InstantiationException;
}
然后StorefrontServiceFactory这个类是这样的:
public class StorefrontServiceFactory implements IStorefrontServiceFactory { // The default is to use the debug implementation
private static String serviceClassname =
"com.oreilly.struts.storefront.service.StorefrontDebugServiceImpl"; private static IStorefrontServiceFactory Singleton = null; public static IStorefrontServiceFactory getInstance() { synchronized (StorefrontServiceFactory.class) {
if (Singleton == null) {
Singleton = new StorefrontServiceFactory();
return Singleton;
}
}
return Singleton;
} private StorefrontServiceFactory() {
init();
} public void init() {
IStorefrontService service = null;
try { service =
(IStorefrontService) Class
.forName(serviceClassname)
.newInstance();
} catch (Exception ex) {

// Swallow the exception and let it occur during the createService() method call.
ex.printStackTrace();
}
} public IStorefrontService createService()
throws
ClassNotFoundException,
IllegalAccessException,
InstantiationException {

return (IStorefrontService) Class
.forName(getServiceClassname())
.newInstance();
} /**
 * @return
 */
public static String getServiceClassname() {
return serviceClassname;
} /**
 * @param string
 */
public static void setServiceClassname(String string) {
serviceClassname = string;
}
}
我的问题是:从factory = StorefrontServiceFactory.getInstance();开始的程序执行顺序是怎么样的?

解决方案 »

  1.   

    怎么没人帮帮我啊,我想知道执行完factory = StorefrontServiceFactory.getInstance()之后,是不是这样执行的:1.调用StorefrontServiceFactory类中的getInstance()方法;2.然后new StorefrontServiceFactory()实例话一个StorefrontServiceFactory对象;3.实例化StorefrontServiceFactory对象的过程中是先调用StorefrontServiceFactory类的init()方法,还是调用StorefrontServiceFactory的接口类IStorefrontServiceFactory中的createService()方法啊?
      

  2.   

    getInstance() ---->new StorefrontServiceFactory();-->init();
      

  3.   

    调用流程:
    1.getInstance;
    2.1如果没有实例化工厂类,则new  StorefrontServiceFactory();
    2.2调用init
    3.如果已实例化了,则直接返回了,不再实例化与init.