mybatis framework decorative pattern

Learning open source framework source code, in addition to knowledge outside the reserve point with the interviewer ignored each other, I think the most important thing is to learn how to write code that Great God, how to do a single responsibility, how to do scalability and so on. . .

Manual carefully to try to summarize what mybatis use of decorative patterns in the cache module.

Perhaps when it comes to decorative pattern decorative pattern will be side-tracked by the four roles, but I think these are Che Dan, no essential scripted, I think myabtis framework is not entirely decorative patterns, one might say is a variation version.

In fact, I think most of the design patterns for nothing more than a combination of interfaces and programming interfaces cited nothing!

 

Let's look at mybatis framework cache module is how to use decorative patterns

1. First, define a cache interface, and features are described in the interface, and mainly putObject getObject

public interface Cache {

  String getId();
  void putObject(Object key, Object value);
  Object getObject(Object key);
  Object removeObject(Object key);
  void clear();
  int getSize();
  default ReadWriteLock getReadWriteLock() {
    return null;
  }

}

 

2. Next, let's look at Cache inheritance system

All-java class, so what did the abstract class or interface, which is decorated with the textbook model a little difference. . . .

 

3. Each Cache Cache implementation classes are in possession of a reference

Class source code look LoggingCache

public class LoggingCache implements Cache {

  private final Log log;
  private final Cache delegate;
  protected int requests = 0;
  protected int hits = 0;

  public LoggingCache(Cache delegate) {
    this.delegate = delegate;
    this.log = LogFactory.getLog(getId());
  }

  @Override
  public String getId() {
    return delegate.getId();
  }

  @Override
  public int getSize() {
    return delegate.getSize();
  }

  @Override
  public void putObject(Object key, Object object) {
    delegate.putObject(key, object);
  }

  @Override
  public Object getObject(Object key) {
    requests++;
    final Object value = delegate.getObject(key);
    if (value != null) {
      hits++;
    }
    if (log.isDebugEnabled()) {
      log.debug("Cache Hit Ratio [" + getId() + "]: " + getHitRatio());
    }
    return value;
  }

  @Override
  public Object removeObject(Object key) {
    return delegate.removeObject(key);
  }

  @Override
  public void clear() {
    delegate.clear();
  }

  @Override
  public int hashCode() {
    return delegate.hashCode();
  }

  @Override
  public boolean equals(Object obj) {
    return delegate.equals(obj);
  }

  private double getHitRatio() {
    return (double) hits / (double) requests;
  }

}

LoggingCache class constructor passed into a Cache delegate, look at the method getObject

 

Such a look, looks like it is consistent with the spiritual core of decorative patterns, but does not copy routine decorative patterns. And ,,, it seems like the chain of responsibility and routine. . . .

Suppose LoggingCache constructor parameters into a FifoCache, and the constructor parameter FifoCache is RedsiCache, it will form a chain when calling LoggingCache # getObject Method:

LoggingCache # getObject () -----------> FifoCache # getObject () --------------------> RedisCache # getObject () (which Do not count the chain of responsibility Well, of course, the real responsibility but also some differences),

And this call chain, LoggingCache # getObject () carried out before, after enhancements, and FifoCache # getObject () - were pre enhancements, of course RedisCache # getObject () is the only query redis library.

 

mybatis framework is one such operation, specifically how this call chain combination, can not see how the Cache Caller is to structure the relationship chain node, using quite flexible, I think it is equivalent to: + decorative chain of responsibility.

The realization of this routine is that all the technology in the implementation class Cache Cache are in possession of an object. Note that the Cache object, rather than a specific object that implements the Cache! 

Perhaps this is the interface for programming it!

After work, you can use it as much as possible, remember!

 

 

 

 

Guess you like

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