设计模式课程 设计模式精讲 4-3 简单工厂源码解析

Posted 1446358788-qq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式课程 设计模式精讲 4-3 简单工厂源码解析相关的知识,希望对你有一定的参考价值。

1    源码解析

1.1  Calendar源码解析

1.2  DriverManager源码解析

 

 

 

1    源码解析
1.1  Calendar源码解析
  /**
     * Gets a calendar using the specified time zone and default locale.
     * The <code>Calendar</code> returned is based on the current time
     * in the given time zone with the default locale. 
     *
     * @param zone the time zone to use
     * @return a Calendar.
     */
    public static Calendar getInstance(TimeZone zone)
    
        return createCalendar(zone, Locale.getDefault());
    

private static Calendar createCalendar(TimeZone zone, Locale aLocale) // If the specified locale is a Thai locale, returns a BuddhistCalendar // instance. if ("th".equals(aLocale.getLanguage()) && ("TH".equals(aLocale.getCountry()))) return new sun.util.BuddhistCalendar(zone, aLocale); else if ("JP".equals(aLocale.getVariant()) && "JP".equals(aLocale.getCountry()) && "ja".equals(aLocale.getLanguage())) return new JapaneseImperialCalendar(zone, aLocale); // else create the default calendar return new GregorianCalendar(zone, aLocale); }

 

1.2  DriverManager源码解析(通过classForName获取)

 

    public static Connection getConnection(String url, 
    java.util.Properties info) throws SQLException 
  
        // Gets the classloader of the code that called this method, may 
    // be null.
    ClassLoader callerCL = DriverManager.getCallerClassLoader();

        return (getConnection(url, info, callerCL));
    

// Worker method called by the public getConnection() methods.
private static Connection getConnection( String url, java.util.Properties info, ClassLoader callerCL) throws SQLException java.util.Vector drivers = null; /* * When callerCl is null, we should check the application‘s * (which is invoking this class indirectly) * classloader, so that the JDBC driver class outside rt.jar * can be loaded from here. */ synchronized(DriverManager.class) // synchronize loading of the correct classloader. if(callerCL == null) callerCL = Thread.currentThread().getContextClassLoader(); if(url == null) throw new SQLException("The url cannot be null", "08001"); println("DriverManager.getConnection(\"" + url + "\")"); if (!initialized) initialize(); synchronized (DriverManager.class) // use the readcopy of drivers drivers = readDrivers; // Walk through the loaded drivers attempting to make a connection. // Remember the first exception that gets raised so we can reraise it. SQLException reason = null; for (int i = 0; i < drivers.size(); i++) DriverInfo di = (DriverInfo)drivers.elementAt(i); // If the caller does not have permission to load the driver then // skip it. if ( getCallerClass(callerCL, di.driverClassName ) != di.driverClass ) println(" skipping: " + di); continue; try println(" trying " + di); Connection result = di.driver.connect(url, info); if (result != null) // Success! println("getConnection returning " + di); return (result); catch (SQLException ex) if (reason == null) reason = ex; // if we got here nobody could connect. if (reason != null) println("getConnection failed: " + reason); throw reason; println("getConnection: no suitable driver found for "+ url); throw new SQLException("No suitable driver found for "+ url, "08001");

 

以上是关于设计模式课程 设计模式精讲 4-3 简单工厂源码解析的主要内容,如果未能解决你的问题,请参考以下文章

设计模式课程 设计模式精讲 6-1 抽象工厂讲解

设计模式课程 设计模式精讲 10-2 外观模式源码解析

设计模式课程 设计模式精讲 16-5 代理模式源码解析

设计模式课程 设计模式精讲 18-3 迭代器模式源码解析

设计模式课程 设计模式精讲 19-3 策略模式源码解析

设计模式课程 设计模式精讲 15-3 桥接模式源码解析