适配器模式相关源代码:slf4j-1.6.1、hibernate-3.6.7

大家都知道。log4j是一个广泛使用的日志工具,除此之外。sun公司在JDK中也有自己的日志工具,也就是java.util.logging.Logger。

当然还有其它一些日志工具。

多种日志工具功能和使用方式相似,一般都包括debug、info、warn、error等日志级别的方法,但却没有实现共同的接口。这一点不像JDBC。尽管关系型数据库种类非常多。比如MySQL、Oracle等,可是有一套统一的接口,也就是JDBC。

当然。假设你自己写一个项目,仅仅要随便找一个日志工具用即可。可是,一些开源框架的作者就比較纠结了。他不知道你的系统用的是哪种日志工具。就不知道他在开源框架中使用哪一个日志工具。

slf4j提供了一个共同的接口。并实现了不同日志工具的适配器。所以开源框架中日志仅仅须要调用slf4j提供的接口即可,不须要关心究竟是用的哪个实现类。比如ORM框架Hibernate的日志就依赖slf4j。

和slf4j相似的还有commons-logging等。

源代码(因为源代码巨长,所下面面省略无关代码):

slf4j提供统一的接口org.slf4j.Logger,该接口提供给client(如Hibernate)去调用:

package org.slf4j;

public interface Logger {

  public boolean isTraceEnabled();

  public void trace(String msg);

  public void trace(String format, Object arg);

  public void trace(String format, Object arg1, Object arg2);

  public void trace(String format, Object[] argArray);

  public void trace(String msg, Throwable t);

  public boolean isDebugEnabled();

  public void debug(String msg);

  public void debug(String format, Object arg);

  public void debug(String format, Object arg1, Object arg2);

  public void debug(String format, Object[] argArray);

  public void debug(String msg, Throwable t);

  public boolean isInfoEnabled();

  public void info(String msg);

  public void info(String format, Object arg);

  public void info(String format, Object arg1, Object arg2);

  public void info(String format, Object[] argArray);

  public void info(String msg, Throwable t);

  public boolean isWarnEnabled();

  public void warn(String msg);

  public void warn(String format, Object arg);

  public void warn(String format, Object[] argArray);

  public void warn(String format, Object arg1, Object arg2);

  public void warn(String msg, Throwable t);

  public boolean isErrorEnabled();

  public void error(String msg);

  public void error(String format, Object arg);

  public void error(String format, Object arg1, Object arg2);

  public void error(String format, Object[] argArray);

  public void error(String msg, Throwable t);

}

在clienthibernate中不是直接调用log4j或JDK logger。而是使用org.slf4j.Logger接口。任取hibernate中有日志的一段代码:

(注:LoggerFactory.getLogger怎样实现本文不须要关注)

package org.hibernate.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public final class SessionFactoryImpl implements SessionFactory, SessionFactoryImplementor { private static final Logger log = LoggerFactory.getLogger(SessionFactoryImpl.class); private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
log.trace("deserializing");
in.defaultReadObject();
log.debug("deserialized: " + uuid);
} private void writeObject(ObjectOutputStream out) throws IOException {
log.debug("serializing: " + uuid);
out.defaultWriteObject();
log.trace("serialized");
} }

而log4j以及JDK的logger并没有实现slf4j的org.slf4j.Logger接口,所以slf4j要提供适配器来实现org.slf4j.Logger接口。由适配器去调用log4j或JDK的logger去实现日志,从而实现日志工具兼容。(注意:源代码中能够看出LocationAwareLogger接口继承org.slf4j.Logger所以实现LocationAwareLogger相当于实现了org.slf4j.Logger)

Log4j适配器org.slf4j.impl.Log4jLoggerAdapter:

package org.slf4j.impl;

import java.io.Serializable;

import org.apache.log4j.Level;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
import org.slf4j.spi.LocationAwareLogger; public final class Log4jLoggerAdapter extends MarkerIgnoringBase implements
LocationAwareLogger, Serializable { final transient org.apache.log4j.Logger logger; public boolean isDebugEnabled() {
return logger.isDebugEnabled();
} public void debug(String msg) {
logger.log(FQCN, Level.DEBUG, msg, null);
} public void debug(String format, Object arg) {
if (logger.isDebugEnabled()) {
FormattingTuple ft = MessageFormatter.format(format, arg);
logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
}
} public void debug(String format, Object arg1, Object arg2) {
if (logger.isDebugEnabled()) {
FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
}
} public void debug(String format, Object[] argArray) {
if (logger.isDebugEnabled()) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());
}
} public void debug(String msg, Throwable t) {
logger.log(FQCN, Level.DEBUG, msg, t);
} }

Jdk logger适配器org.slf4j.impl.JDK14LoggerAdapter:

package org.slf4j.impl;

import java.util.logging.Level;

import org.slf4j.Marker;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MarkerIgnoringBase;
import org.slf4j.helpers.MessageFormatter;
import org.slf4j.spi.LocationAwareLogger; public final class JDK14LoggerAdapter extends MarkerIgnoringBase implements
LocationAwareLogger { final java.util.logging.Logger logger; public boolean isDebugEnabled() {
return logger.isLoggable(Level.FINE);
} public void debug(String msg) {
if (logger.isLoggable(Level.FINE)) {
log(SELF, Level.FINE, msg, null);
}
} public void debug(String format, Object arg) {
if (logger.isLoggable(Level.FINE)) {
FormattingTuple ft = MessageFormatter.format(format, arg);
log(SELF, Level.FINE, ft.getMessage(), ft.getThrowable());
}
} public void debug(String format, Object arg1, Object arg2) {
if (logger.isLoggable(Level.FINE)) {
FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
log(SELF, Level.FINE, ft.getMessage(), ft.getThrowable());
}
} public void debug(String format, Object[] argArray) {
if (logger.isLoggable(Level.FINE)) {
FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
log(SELF, Level.FINE, ft.getMessage(), ft.getThrowable());
}
} public void debug(String msg, Throwable t) {
if (logger.isLoggable(Level.FINE)) {
log(SELF, Level.FINE, msg, t);
}
} }

在适配器模式中,一般包括下面几个部分:

Adaptee:真正实现功能的实现类,可是与client不兼容。也就是上面代码中的java.util.logging.Logger、org.apache.log4j.Logger。

Target:给client调用的接口,适配器实现这个接口。就是上面代码中的org.slf4j.Logger。

Adapter:适配器,适配器实现Target接口,详细功能调用Adaptee来实现。就是上面的org.slf4j.impl.Log4jLoggerAdapter、org.slf4j.impl.JDK14LoggerAdapter。

Client:调用Target接口。

就是上面的Hibernate。

总结:

有多个相似的类(比如java.util.logging.Logger、org.apache.log4j.Logger),没有统一的接口,可是client又都想要兼容。遇到这样的情况,最好的办法是重构,也就是让他们实现同一接口。可是假设重构成本太大或者根本就无法实现同一接口(比如上面的样例。log4j和JDK logger根本就不是一家的),就必须创造出统一的接口(即org.slf4j.Logger)。并为每一个类写一个适配器(即org.slf4j.impl.Log4jLoggerAdapter、org.slf4j.impl.JDK14LoggerAdapter)。让适配器来实现统一的接口,并调用详细的实现类来实现,已达到兼容的目的。

作者:叉叉哥   转载请注明出处:http://blog.csdn.net/xiao__gui/article/details/32695647

Java读源代码学设计模式:适配器Adapter的更多相关文章

  1. 设计模式--适配器(Adapter)模式

    今天学习另一个设计模式,适配器(Adapter)模式,这是一个共同方向,但有特殊要求,就应用到此设计模式.写到这里,想起很久以前,有写过一篇<ASP.NET的适配器设计模式(Adapter)&g ...

  2. Objective-C设计模式——适配器Adapter(接口适配)

    适配器模式 适配器模式通俗来讲,其实就是对客户端添加新的类但却不修改客户端和新的类的接口.此时我们需要自己来实现适配,在适配器模式中有Target对象,即客户端所需要的接口对象,Adaptee对象,即 ...

  3. 设计模式 适配器-Adapter

    适配器模式:将一个类的接口,转换成客户期望的另一个接口.适配器让原本接口不兼容的类可以合作无间. 直接上图.下面是对象适配器的类图.由于Java不支持多继承.所以这是Java的适配器实现方式. 结合H ...

  4. Ruby设计模式透析之 —— 适配器(Adapter)

    转载请注明出处:http://blog.csdn.net/sinyu890807/article/details/9400153 此为Java设计模式透析的拷贝版,专门为Ruby爱好者提供的,不熟悉R ...

  5. 设计模式学习心得<适配器 Adapter>

    适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁.这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能. 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接 ...

  6. Java学习-005-初学常用的几个经典循环控制源代码

    最近一段时间公司 App 改版,一直处在需求评审.代码评审.测试计划.测试用例.用例评审.用例执行.缺陷管理.测试总结的循环中,因而博客也好久没有更新了.虽然工作确实忙了点,但是也是自己懒惰了,从今天 ...

  7. 2、适配器 adapter 模式 加个"适配器" 以便于复用 结构型设计模式

    1.什么是适配器模式? 适配器如同一个常见的变压器,也如同电脑的变压器和插线板之间的电源连接线,他们虽然都是3相的,但是电脑后面的插孔却不能直接插到插线板上. 如果想让额定工作电压是直流12伏特的笔记 ...

  8. 黑马程序员——JAVA基础之简述设计模式

    ------- android培训.java培训.期待与您交流! ---------- 设计模式(Design Patterns) 设计模式(Design pattern)是一套被反复使用.多数人知晓 ...

  9. Java实现23种设计模式

    一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接 ...

随机推荐

  1. go 条件语句if

    一.if 语句 格式 if condition { // do something } 举例 package main import "fmt" func main(){ var ...

  2. SQL Server跨库跨服务器访问实现

    我们经常会遇到一个数据库要访问另一个数据库,或者一台服务器要访问另一台服务器里面的数据库. 那么这个如何实现的呢? 相信看完这篇文章你就懂了! 同一台服务器跨库访问实现 1. 首先创建两个数据库Cro ...

  3. 5.26 Quartz任务调度图解2

  4. Asp.net MVC4 Step by Step (2)-参数数据的传递

    首先创建一个表单,不同于WebForm,框架提供了一系列HMTL帮助方法来生成HTML标签. 下面创建一个Create.cshtml为文件名的视图. <h2> Create Auction ...

  5. Assembly之example

    Here is a simple example by assembly language. It is based on openMSP430. Very important is to under ...

  6. Python之global

    1 Global The global statement and its nonlocal cousin are the only things that are remotely like dec ...

  7. 读书笔记「Python编程:从入门到实践」_8.函数

    8.1 定义函数 def greet_user(): # def 来告诉Python你要定义一个函数.这是函数定义 """Hello World""& ...

  8. MOOC推荐及三门基础学科

    top1:学堂在线 http://www.xuetangx.com/ top2:网易云课堂 http://study.163.com/ top3:coursera https://www.course ...

  9. CF319E Ping-Pong 线段树 + vector + 思维

    Code: #include<bits/stdc++.h> #define N 3000009 #define maxn 3000009 #define ll long long #def ...

  10. 上传菜品数据&生成点餐二维码

    基础数据上传 在门店助手打开数据上传功能,点击上传到微餐厅3.0,,即将门店本地的基础数据上传到线上. 注意1:上传前,需要在线下系统维护好基础数据 注意2:线下基础数据发生更改时,需要手动在门店助手 ...