其中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();开始的程序执行顺序是怎么样的?
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.getInstance;
2.1如果没有实例化工厂类,则new StorefrontServiceFactory();
2.2调用init
3.如果已实例化了,则直接返回了,不再实例化与init.