看了一下jdk的代码,大概是这样 在URL()中有一 URLStreamHandler handler对象; 在URL构造时 根据条件为handler 附值 根据文档--- for example, for HTTP an HttpURLConnection will be returned, and for JAR a JarURLConnection will be returned. handler 可以是HttpURLConnection 或者JarURLConnection 所以这个时候URLConnection yc=HttpURLConnection 抽象类可以是子类的引用 不过 yc.getInputStrea()这一句反倒没看懂
在URL()中有一
URLStreamHandler handler对象;
在URL构造时 根据条件为handler 附值 根据文档---
for example, for HTTP an
HttpURLConnection will be returned, and for JAR a
JarURLConnection will be returned.
handler 可以是HttpURLConnection 或者JarURLConnection
所以这个时候URLConnection yc=HttpURLConnection 抽象类可以是子类的引用
不过 yc.getInputStrea()这一句反倒没看懂
他随便这样写一下不就有了一个子类
return new sun.net.www.protocol.http.HttpURLConnection();
当然这是一个象征性的写法。
2.sun.net.www.protocol.http.HttpURLConnection
这段说明我也看到了,但是handler 在代码中找不到与HttpURLConnection 或者JarURLConnection 类有任何关系的啊to linux_328(企鹅)
JAVA与模式中说这是个工厂方法模式。to funcreal(为中华之崛起而编程)
关键是 代码中没有return new sun.net.www.protocol.http.HttpURLConnection();
但是yahoo.openConnection();怎么就返回HttpURLConnection类型了呢?
if (handler == null) { boolean checkedWithFactory = false; // Use the factory (if any)
if (factory != null) {
handler = factory.createURLStreamHandler(protocol);
checkedWithFactory = true;
} // Try java protocol handler
if (handler == null) {
String packagePrefixList = null; packagePrefixList
= (String) java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction(
protocolPathProp,""));
if (packagePrefixList != "") {
packagePrefixList += "|";
} // REMIND: decide whether to allow the "null" class prefix
// or not.
packagePrefixList += "sun.net.www.protocol"; StringTokenizer packagePrefixIter =
new StringTokenizer(packagePrefixList, "|"); while (handler == null &&
packagePrefixIter.hasMoreTokens()) { String packagePrefix =
packagePrefixIter.nextToken().trim();
try {
String clsName = packagePrefix + "." + protocol +
".Handler";
Class cls = null;
try {
cls = Class.forName(clsName);
} catch (ClassNotFoundException e) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
if (cl != null) {
cls = cl.loadClass(clsName);
}
}
if (cls != null) {
handler =
(URLStreamHandler)cls.newInstance();
}
} catch (Exception e) {
// any number of exceptions can get thrown here
}
}
} synchronized (streamHandlerLock) { URLStreamHandler handler2 = null; // Check again with hashtable just in case another
// thread created a handler since we last checked
handler2 = (URLStreamHandler)handlers.get(protocol); if (handler2 != null) {
return handler2;
} // Check with factory if another thread set a
// factory since our last check
if (!checkedWithFactory && factory != null) {
handler2 = factory.createURLStreamHandler(protocol);
} if (handler2 != null) {
// The handler from the factory must be given more
// importance. Discard the default handler that
// this thread created.
handler = handler2;
} // Insert this handler into the hashtable
if (handler != null) {
handlers.put(protocol, handler);
} }
} return handler; }String clsName = packagePrefix + "." + protocol + ".Handler";其中 packagePrefix = "sun.net.www.protocol";
protocol = “http”但是 Class cls = Class.forName("sun.net.www.protocol.http.Handler"); 这个是什么类呢
能否再讲详细些啊,谢谢了
但是我现在就不清楚在那里实列化成 HttpURLConnection 的呢
我觉得是个工厂方法的single模式,防止对同一个url产生多个connection。
大家觉的呢?
abstract factory
factory builder
factory method
singleton
prototype