AbstractFactory模式和可擴展性
如果要實現較好的可擴展性
在AuthorizationFactory中使用一個private static變量factory
private static AuthorizationFactory factory = null;
用一個private static的String
private static String className =
然後是用一個private static的loadAuthorizationFactory方法來給這個factory變量賦值
private static void loadAuthorizationFactory() {
if (factory == null) {
synchronized(className) {
if (factory == null) {
String classNameProp = PropertyManager
);
if (classNameProp != null) {
className = classNameProp;
}
try {
Class c = Class
factory = (AuthorizationFactory)c
}
catch (Exception e) {
System
e
}
}
}
}
}
在static的getAuthorization方法返回一個Authorization的過程中
public static Authorization getAuthorization(String username
String password) throws UnauthorizedException
{
loadAuthorizationFactory();
return factory
}
不同的子類有不同的createAuthorization方法的實現
public Authorization createAuthorization(String username
throws UnauthorizedException
{
if (username == null || password == null) {
throw new UnauthorizedException();
}
password = StringUtils
int userID =
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager
pstmt = con
pstmt
pstmt
ResultSet rs = pstmt
if (!rs
throw new UnauthorizedException();
}
userID = rs
}
catch( SQLException sqle ) {
System
sqle
throw new UnauthorizedException();
}
finally {
try { pstmt
catch (Exception e) { e
try { con
catch (Exception e) { e
}
return new DbAuthorization(userID);
}
在這個類中
還有就是靜態方法的使用
在AuthorizationFactory中定義的其它方法
這樣
其它的
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19344.html