Framework adapter mode MyBatis

MyBatis framework with multiple use design patterns, we read its source, the need to use its good experience design patterns, in order to shining painted gourd dipper. Data in this chapter is to use the adapter mode recording it.

Adapter mode is very simple to understand, is equivalent to using an interface, the old-packaged features that make it adapt to the new demands (probably a mistake, but it would be wrong).

MyBatis framework for the use of adapters, mainly in Log log this one.

 

MyBatis from a definition of a Log interface, the interface Well, regulate it. And I do not care but also how to operate, just need to follow my specifications.

 

/ ** 
 Log * MyBatis interface specification definition 
 * / 
public  interface the Log { 

  Boolean IsDebugEnabled (); 

  Boolean isTraceEnabled (); 

  void error (String S, the Throwable E); 

  void error (String S); 

  void Debug (String S); 

  void the trace (String S); 

  void The warn (String S); 

}

 

This interface defines the log method Mybatis direct use, and the interface specific implementation Log Who does? Mybatis provided to achieve a variety of logging framework, these implementations are matching the Log interface methods defined by the interface, and ultimately to all external frame adapted Mybatis log logging package.

MyBatis is so just, offers so many implementations. What do you mean, that is so much Log interface adapter logging framework, we use, only just introduced one of the logging framework, you can use the Log interface specification to operate the log.

 

Let's look at a specific implementation. After all, what matter is the interface could not do, and have to rely on the Director-General to achieve it!

For example with log4j

Import org.apache.ibatis.logging.Log;
 Import org.apache.log4j.Level;
 Import the org.apache.log4j.Logger; 

public  class Log4jImpl the implements the Log { 

  Private  static  Final . FQCN of String = Log4jImpl class .getName (); 

  / / log log4j instance of an object frame is defined as a global variable, so that other methods operating in class 
  Private  Final Logger log; 
  
  / ** 
   * Create real example of the operation log by the structure, here the apache log4j 
   * / 
  public Log4jImpl (String clazz) {
    log = Logger.getLogger (clazz); // real things Officer 
  } 

  / **
   The method of IsDebugEnabled * Log interface method calls MyBatis isDebugEnabled, its essence is to call log object apache log4j 
   * other methods are the same principles 
   * / 
@Override public Boolean IsDebugEnabled () { return log.isDebugEnabled (); } @Override public Boolean isTraceEnabled ( ) { return log.isTraceEnabled (); } @Override public void error (String S, the Throwable E) { log.log (FQCN of, Level.ERROR, S, E); } @Override public void error (String S) { log .log (FQCN of, Level.ERROR, S, null); } @Override public void Debug (String S) { log.log (FQCN of, Level.DEBUG, S, null); } @Override public void trace(String s) { log.log(FQCN, Level.TRACE, s, null); } @Override public void warn(String s) { log.log(FQCN, Level.WARN, s, null); } }

 

Another example is another implementation of adaptation jdk log

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.ibatis.logging.Log;

/**
 * @author Clinton Begin
 */
public class Jdk14LoggingImpl implements Log {

  private final Logger log;

  public Jdk14LoggingImpl(String clazz) {
    log = Logger.getLogger(clazz);  // 真正干事的东西
  }

  @Override
  public boolean isDebugEnabled() {
    return log.isLoggable(Level.FINE);
  }

  @Override
  public boolean isTraceEnabled() {
    return log.isLoggable(Level.FINER);
  }

  @Override
  public void error(String s, Throwable e) {
    log.log(Level.SEVERE, s, e);
  }

  @Override
  public void error(String s) {
    log.log(Level.SEVERE, s);
  }

  @Override
  public void debug(String s) {
    log.log(Level.FINE, s);
  }

  @Override
  public void trace(String s) {
    log.log(Level.FINER, s);
  }

  @Override
  public void warn(String s) {
    log.log(Level.WARNING, s);
  }

}

The code is simple, the principle is the same the same.

 

to sum up:

  Adapter pattern is so simple code routine is to first create an interface, a variety of interface implementation subclasses are coerced the real target of the Director-General. Why call adapter model? It is estimated that the caller will be associated with instances of real-General through this interface. I am not feeling a little decoupling mean? I think there is something there, but its essence is an instance of a variety of children's Director-General, regulate through an interface to facilitate the use of the caller. That is, the caller does not have to be concerned about the implementation.

  It is not that a bit like a facade pattern, is not yo! 

  Is not that a bit like a strategy pattern, I think there are so mean. . . . But there are still differences. 

  

 

  

 

 

  

Guess you like

Origin www.cnblogs.com/z-qinfeng/p/12215988.html