简单工厂(三)——JDK源码中的简单工厂
private static Calendar createCalendar(TimeZone zone,Locale aLocale)
{
CalendarProvider provider =
LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale)
.getCalendarProvider();
if (provider != null) {
try {
return provider.getInstance(zone, aLocale);
} catch (IllegalArgumentException iae) {
// fall back to the default instantiation
}
} Calendar cal = null;
/*根据不同的地区来创建不同的日历对象,就好比日历这个工厂,生产着世界上各地区的日历,我需要这个地区日历,我只需要传参数告诉工厂即可,不需要知道日历制作过程和实例的过程*/
if (aLocale.hasExtensions()) {
String caltype = aLocale.getUnicodeLocaleType("ca");
if (caltype != null) {
switch (caltype) {
case "buddhist":
cal = new BuddhistCalendar(zone, aLocale);
break;
case "japanese":
cal = new JapaneseImperialCalendar(zone, aLocale);
break;
case "gregory":
cal = new GregorianCalendar(zone, aLocale);
break;
}
}
}
if (cal == null) {
// If no known calendar type is explicitly specified,
// perform the traditional way to create a Calendar:
// create a BuddhistCalendar for th_TH locale,
// a JapaneseImperialCalendar for ja_JP_JP locale, or
// a GregorianCalendar for any other locales.
// NOTE: The language, country and variant strings are interned.
if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") {
cal = new BuddhistCalendar(zone, aLocale);
} else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja"
&& aLocale.getCountry() == "JP") {
cal = new JapaneseImperialCalendar(zone, aLocale);
} else {
cal = new GregorianCalendar(zone, aLocale);
}
}
return cal;
}

Calendar类图
Class.forName("com.mysql.jdbc.Driver");
@CallerSensitive
public static Connection getConnection(String url)
throws SQLException {
java.util.Properties info = new java.util.Properties();
return (getConnection(url, info,Reflection.getCallerClass()));
}
// Worker method called by the public getConnection() methods.
private static Connection getConnection(
String url, java.util.Properties info, Class<?> caller) throws SQLException {
/*
* 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.
*/
ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
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 + "\")"); // Walk through the loaded registeredDrivers attempting to make a connection.
// Remember the first exception that gets raised so we can reraise it.
SQLException reason = null; for(DriverInfo aDriver : registeredDrivers) {
// If the caller does not have permission to load the driver then
// skip it.
if(isDriverAllowed(aDriver.driver, callerCL)) {
try {
println(" trying " + aDriver.driver.getClass().getName());
Connection con = aDriver.driver.connect(url, info);
if (con != null) {
// Success!
println("getConnection returning " + aDriver.driver.getClass().getName());
return (con);
}
} catch (SQLException ex) {
if (reason == null) {
reason = ex;
}
} } else {
println(" skipping: " + aDriver.getClass().getName());
} } // 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");
}
// Walk through the loaded registeredDrivers attempting to locate someone
// who understands the given URL.
for (DriverInfo aDriver : registeredDrivers) {
// If the caller does not have permission to load the driver then
// skip it.
if(isDriverAllowed(aDriver.driver, callerClass)) {
try {
if(aDriver.driver.acceptsURL(url)) {
// Success!
println("getDriver returning " + aDriver.driver.getClass().getName());
return (aDriver.driver);
} } catch(SQLException sqe) {
// Drop through and try the next driver.
}
} else {
println(" skipping: " + aDriver.driver.getClass().getName());
} }
因为这个可以看出来它是一个for循环,在遍历注册的一个驱动,
private final static CopyOnWriteArrayList<DriverInfo> registeredDrivers = new CopyOnWriteArrayList<>();
并且它是CopyOnWriteArrayList,里面是DriverInfo,初始化的时候他是一个空的,具体是什么时候完成注册的呢,
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
public static synchronized void registerDriver(java.sql.Driver driver,
DriverAction da)
throws SQLException {
/* Register the driver if it has not already been added to our list */
if(driver != null) {
registeredDrivers.addIfAbsent(new DriverInfo(driver, da));
} else {
// This is for compatibility with the original DriverManager
throw new NullPointerException();
}
println("registerDriver: " + driver);
}
如果不存在就往里放
if(driver != null) {
registeredDrivers.addIfAbsent(new DriverInfo(driver, da));
}
此外,logback中也有简单工厂的影子。
public final class LoggerFactory
public static ILoggerFactory getILoggerFactory() {
}
最里面有一个getLogger方法,
public static Logger getLogger(Class clazz) {
return getLogger(clazz.getName());
}
public static Logger getLogger(String name) {
ILoggerFactory iLoggerFactory = getILoggerFactory();
return iLoggerFactory.getLogger(name);
}
这个还有个重载,一个是String name,还有一个是Class clazz,clazz是干嘛的,是clazz.getName(),
首先getLogger根据我们传来的name,从iLoggerFactory里面getLogger,先看一下
public interface ILoggerFactory
package org.slf4j;
//抽象产品工厂
public interface ILoggerFactory {
//抽象工厂方法
public Logger getLogger(String name);
}
很明显ILoggerFactory它是一个接口,下面有一个方法,那这个呢是工厂方法,那在后面我们也会讲,这里先过去,
后面我们学习工厂方法的时候,再单独来说,然后通过iLoggerFactory.getLogger,因为它是一个接口,肯定有多个实现,
LoggerContext
//具体工厂实现类
public class LoggerContext extends ContextBase implements ILoggerFactory, LifeCycle {
...
//具体工厂方法
@Override
public final Logger getLogger(final String name) {
...
}
这里面我们看一下,传入的一个name,这里面要返回Logger,这里面对name进行了判断,很明显这个方法就是一个简单工厂方法,
根据传入的入参进行选择哪个Logger,那这个还是非常简单的,刚刚也说了,在我们的LoggerFactory里面,既存在了工厂方法,
又存在了简单工厂,所以设计模式在使用的时候,不一定要局限在使用一种,例如这里就是一个组合的使用,这个简单工厂比较简单,
在很多源码中也能够找到他的影子,在前面的JDK,Logback开源框架的,对于学习设计模式的讲解呢,我们在阅读源码的时候呢,
还可以以设计模式的角度,去聚焦源码,这样对我们理解源码也是有益处的 。
简单工厂(三)——JDK源码中的简单工厂的更多相关文章
- 结合JDK源码看设计模式——简单工厂、工厂方法、抽象工厂
三种工厂模式的详解: 简单工厂模式: 适用场景:工厂类负责创建的对象较少,客户端只关心传入工厂类的参数,对于如何创建对象的逻辑不关心 缺点:如果要新加产品,就需要修改工厂类的判断逻辑,违背软件设计中的 ...
- 设计模式-简单工厂Coding+jdk源码解析
感谢慕课geely老师的设计模式课程,本套设计模式的所有内容均以课程为参考. 前面的软件设计七大原则,目前只有理论这块,因为最近参与项目重构,暂时没有时间把Coding的代码按照设计思路一点点写出来. ...
- JDK源码中,都有哪些NB的设计模式?
转载:https://mp.weixin.qq.com/s/h88UxB9F2MkTbHqck3KQiQ 一.结构性模式: 1.适配器模式: 常用于将一个新接口适配旧接口 肥朝小声逼逼:在我们业务代码 ...
- Java1.8 JDK源码中,对两个类进行 按位与 操作是什么意思
Java容器类库中的Map接口(java\util\Map.java)中有一个Entry接口(java\util\Map.java),其中有几个接口方法用到了类和类的按位与操作,即类和类之间有 &am ...
- JDK源码中使用的设计模式
结构型模式: 适配器模式: 用来把一个接口转化成另一个接口. java.util.Arrays#asList() javax.swing.JTable(TableModel) java.io.Inpu ...
- JDK源码中的英文注释翻译(Enum<E extends Enum<E>>)
public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializab ...
- JDK源码中的英文注释翻译(Class)
public final class Class<T> implements java.io.Serializable, GenericDeclaration, Type, Annotat ...
- 关于JDK源码:我想聊聊如何更高效地阅读
简介 大家好,我是彤哥,今天我想和大家再聊聊JDK源码的几个问题: 为什么要看JDK源码 JDK源码的阅读顺序 JDK源码的阅读方法 为什么要看JDK源码 一,JDK源码是其它所有源码的基础,看懂了J ...
- Java设计模式:23种设计模式全面解析(超级详细)以及在源码中的应用
从网络上找的设计模式, 很全面,只要把UML类图看懂了, 照着类图将代码实现是很容易的事情. 步骤: 先看懂类图, 然后将代码实现, 之后再看文字 http://c.biancheng.net/des ...
随机推荐
- Ubuntu 18.04 环境下安装 Matlab2018
由于实验环境要求,最近在 Ubuntu 18.04 上安装了 Matlab2018b , 这里简单记录过程. (1) 首先是获取对应的 Matlab2018b 的安装包,这里笔者是在一个外国的网站上获 ...
- 算法学习笔记——sort 和 qsort 提供的快速排序
这里存放的是笔者在学习算法和数据结构时相关的学习笔记,记录了笔者通过网络和书籍资料中学习到的知识点和技巧,在供自己学习和反思的同时为有需要的人提供一定的思路和帮助. 从排序开始 基本的排序算法包括冒泡 ...
- 20180516模拟赛T1——queen
题解 这题显然是\(总方案数不可行方案数总方案数-不可行方案数\)(直接算是无规则的).总方案数是\(n^2m^2\),于是问题就在于不可行的方案数. 若queen落在一个点上,则横竖是十分好求的(\ ...
- Ruby——报错总结
前言 记录ruby的一些报错 错误 Could not find a valid gem 'pumagem' (>= 0) in any repository ERROR: Could not ...
- 将Eclipse设置为黑色主题
将Eclipse设置为黑色主题 觉得黑色的主题&配色很高大上,于是花了点时间实践出下面一种方法. 修改代码编辑区配色 修改整个软件主题 先上成果图: 但是进度条依旧是白色的,不知道怎么弄了╮( ...
- Hbase扩展
1 HBase在商业项目中的能力 每天: 1) 消息量:发送和接收的消息数超过60亿 2) 将近1000亿条数据的读写 3) 高峰期每秒150万左右操作 4) 整体读取数据占有约55%,写入占有45% ...
- iis webapi不间隔第一次访问超慢
第一种尝试(正在验证是否有效): 设置网站对应的应用程序池的"闲置超时"为0.如下图所示
- Numpy | 11 迭代数组
NumPy 迭代器对象 numpy.nditer 提供了一种灵活访问一个或者多个数组元素的方式. 迭代器最基本的任务的可以完成对数组元素的访问. 实例1:使用 arange() 函数创建一个 2x3 ...
- 浏览器正在等待locatehost的响应
1.问题描述 在进行了几次增删改查操作后,浏览器显示浏览器正在等待locatehost的响应: 2.错误原因: 对数据库为关闭相应的资源. 3.解决方案: 关闭JDBC开启的资源:
- VS2017 远程调试linux(centos).net core程序(通过附加程序的方式)
参考两位大神的帖子: https://blog.csdn.net/soband_xiang/article/details/82914195 https://blog.csdn.net/weixin_ ...