设计模式课程 设计模式精讲 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");
    }

原文地址:https://www.cnblogs.com/1446358788-qq/p/11259551.html

时间: 2024-08-29 12:08:03

设计模式课程 设计模式精讲 4-3 简单工厂源码解析的相关文章

设计模式课程 设计模式精讲 6-3 抽象工厂源码解析

1 源码解析 1.1 mysql源码解析 1.2 mybaties 的sqlsession源码解析 1 源码解析 1.1 mysql源码解析 1.2 mybaties 的sqlsession源码解析 原文地址:https://www.cnblogs.com/1446358788-qq/p/11295158.html

设计模式课程 设计模式精讲 12-3 适配器模式源码解析

1 源码解析 1.1 源码解析1(在jdk中的应用) 1.2 源码解析2(Spring中的通知管理) 1.3 源码解析3(SpringMVC中的应用) 1 源码解析 1.1 源码解析1(在jdk中的应用) xmlAdapter(此类是用于适配xml的一个类,是处理xml序列化和反序列化的一个类) public abstract class XmlAdapter<ValueType,BoundType> { /** * Do-nothing constructor for the derived

设计模式课程 设计模式精讲 17-3 模板方法模式源码解析

1 源码解析 1.1 源码解析1(在jdk中的使用) 1.2 源码解析2(在servlet中的应用) 1.3 源码解析3(在mybaties中的应用) 1 源码解析 1.1 源码解析1(在jdk中的使用) AbstractList(父类) public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { //get方法为抽象方法,完全交给子类去实现 abstr

设计模式-简单工厂Coding+jdk源码解析

前面的软件设计七大原则,目前只有理论这块,因为最近参与项目重构,暂时没有时间把Coding的代码按照设计思路一点点写出来.本周周末会花时间整理出来,然后现在想的是白天更新一点并发编程,晚上回家学习设计模式.非科班出身,脑子也比较笨.博文都是自己根据学习的时候所想的思路,如果能有帮到各位的地方,那十分荣幸.如果有欠缺之处,希望能在评论中指出一起进步.好啦,开始正文了. 本套设计模式的博文,包含各种设计模式的定义.类型.适用场景及优缺点分析.并通过Coding去实际加深理论理解. 简单工厂: 该模式

设计模式(四):SIMPLE FACTORY简单工厂模式 -- 创建型模式

1.定义 简单工厂模式又称静态工厂方法模式.重命名上就可以看出这个模式一定很简单.它存在的目的很简单:定义一个用于创建对象的接口. 2.适用场景 如果一个客户要一款宝马车,一般的做法是客户去创建一款宝马车,然后拿来用.后来出现工业革命.用户不用去创建宝马车.因为客户有一个工厂来帮他创建宝马.想要什么车,这个工厂就可以建.比如想要320i系列车.工厂就创建这个系列的车.即工厂可以创建产品. 3.评价 优点: 工厂类是整个模式的关键.包含了必要的逻辑判断,根据外界给定的信息,决定究竟应该创建哪个具体

Android设计模式源码解析之观察者模式

Android设计模式源码解析之观察者模式 本文为 Android 设计模式源码解析 中 观察者模式 分析 Android系统版本: 2.3 分析者:Mr.Simple,分析状态:未完成,校对者:Mr.Simple,校对状态:未开始 1. 模式介绍 模式的定义 定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都会得到通知并被自动更新. 模式的使用场景 关联行为场景.需要注意的是,关联行为是可拆分的,而不是"组合"关系: 事件多级触发场景: 跨系统的消息交换

高仿精仿饭否客户端应用源码

今天给大家介绍一款非常不错的饭否客户端应用源码,该应用跟现实中的饭否应用功能差不错,界面也很类似,基本实现了饭否应用的功能,喜欢的朋友可以下载学习看看,希望大家功能喜欢. 应用预览图:<ignore_js_op> <ignore_js_op> <ignore_js_op> FanfouData.cs:下载解析返回的数据,数据格式为xml FanfouStatus.cs:返回"随便看看"数据的模型 FnafouUser.cs:饭否用户的数据模型 WP7

c 语言简单计算器源码

//  main.c //  计算器 //  Created by qianfeng on 14-7-15. //  Copyright (c) 2014年 ___FGY___. All rights reserved. //iPhone自带计算器不够好,由于你技术出众,你被安排去开发一款iOS新式计算器. /*项目经理认为计算器第一版要支持表达式求值,所以要求如下: 输入任意表达式 求出他的值(支持负数,不支持小数) 这里支持6种表达式 () * / + - ()优先级最高, * /优先级其次

Scala 深入浅出实战经典 第48讲:Scala类型约束代码实战及其在Spark中的应用源码解析

王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 腾讯微云:http://url.cn/TnGbdC 360云盘:http://yunpan.cn/cQ4c2UALDjSKy 访问密码 45e2 技术爱好者尤其是大数据爱好者 可以加DT大数据梦工厂的qq群 DT大数据梦工厂① :462923555 DT大数据梦工厂②:437123764 DT大数据梦工厂③