Decorator pattern of design pattern (mybatis source code analysis)

Time:2022-1-4

Understand, what is decorator mode?

1. Definitions

Decoration mode is to dynamically extend the function of an object without changing the original class file and using inheritance. It is
  Wrap the real object by creating a wrapper object, that is, decoration.

2. Features

Decorative objects and real objects have the same interface. In this way, client objects can be and decorated in the same way as real objects
    Like interaction.
  2. The decoration object contains a reference to a real object
  3. The decoration object accepts all requests from the client. It forwards these requests to real objects.
  4 decoration objects can add some additional functions before or after forwarding these requests. This ensures that at run time, no
    Modifying the structure of a given object can add additional functions externally. In object-oriented design, it is usually through inheritance
    To realize the function extension of a given class.

3. Applicability

1. You need to extend the function of a class or add additional responsibilities to a class.
  2. You need to dynamically add functions to an object, and these functions can be dynamically revoked.
  3. It is necessary to add a large number of functions generated by the arrangement and combination of some basic functions, so as to make the inheritance relationship disappear
     Real.
  4. When the method of generating subclasses cannot be used for expansion. In one case, there may be a large number of independent extensions to support each
     A combination will produce a large number of subclasses, making the number of subclasses increase explosively. Another case may be because of the class definition
     Is hidden, or the class definition cannot be used to generate subclasses.

4. Advantages

1. The purpose of decorator pattern and inheritance relationship is to extend the function of objects, but decorator can provide comparison and inheritance
     More flexibility.
  2. By using different specific decorative categories and the arrangement and combination of these decorative categories, designers can create many different behaviors
     A combination of.

5. Disadvantages

1. This feature, which is more flexible than inheritance, also means more complexity.
  2. Decoration mode will lead to many small classes in the design. If it is overused, the program will become very complex.
  3. Decoration mode is programming for abstract component type. However, if you want to program for specific components,
     You should rethink your application architecture and whether the decorator is appropriate. Of course, you can also change the component interface,
     Add new public behavior to realize the "translucent" decorator mode. Make the best choice in the actual project.

6. Model simplification

1. If there is only one concrete component class and there is no abstract component interface, the decorator can be used
     Inherit concrete component.
  2. If there is only one concrete decorator class, you can combine decorator and concrete decorator 
     And.

Mybatis source code analysis

As shown in the figure:

Decorator pattern of design pattern (mybatis source code analysis)

In the figure, the interface class cache is implemented by all classes
Here is a special class, perpetualcache class

Directory structure:

Decorator pattern of design pattern (mybatis source code analysis)

Class structure

Because there are too many classes, only three class structure diagrams are shown here

Decorator pattern of design pattern (mybatis source code analysis)
Decorator pattern of design pattern (mybatis source code analysis)
Decorator pattern of design pattern (mybatis source code analysis)

In this way, we all know about these categories and decorators.

explain

In mybatis, the cache function is defined by the interface cache class, and the decorator design pattern is used. The storage and cache functions are defined by
Implement the perpetualcache class, and then control the cache policy of the perpetualcache class through other decorators.
    As shown in the figure above, it can be understood that perpetualcache is the base class, and other implemented cache classes are extensions of the base class
Exhibition, that is, decoration to wrap real objects.
    The function of the class is extended, or some methods are added. So it has good flexibility.

explain

There are 8 standard decorators for decorating the perpetual cache (all in the org.apache.ibatis.cache.decorators package):

1. Fifocache: FIFO algorithm, cache recycling strategy
     2. Loggingcache: output the log information of cache hits
     3. Lrucache: least recently used algorithm, cache recycling strategy
     4. Scheduledcache: scheduling cache, which is responsible for clearing the cache regularly
     5. Serializedcache: cache serialization and deserialization storage
     6. Softcache: cache management strategy based on soft reference
     7. Synchronized cache: a synchronized cache decorator used to prevent concurrent access by multiple threads
     8. Weakcache: cache management strategy based on weak reference

Additional:

A special decorator, transactional cache: transactional cache

Mybatis cache is also divided into L1 cache and L2 cache:
  • The L1 cache, also known as the local cache, is a permanent cache of type perpetualcache, which is stored in the executor
    (baseexecutor), and the executor is in sqlsession (defaultsqlsession), so
    The lifecycle of the L1 cache is the same as that of sqlsession.
  • L2 cache, also known as user-defined cache, can be configured because all classes that implement the cache interface can be used as L2 cache
    Set a third-party cache such as encache. The L2 cache takes the namespace namespace as its unique identifier and is protected
    Exists in the configuration core configuration object

be careful:

The default type of L2 cache object is perpetual cache. If the configured cache is the default type, mybatis
 A series of decorators will be automatically added according to the configuration.

The reference order between cache objects is:

 SynchronizedCache–>LoggingCache–>SerializedCache–>ScheduledCache–>LruCache–>PerpetualCache

Mybatis source code—— https://gitee.com/SmileSnake/…

Parameter data
Decorator pattern concept
If there is infringement, delete it immediately

Recommended Today

Summary of import and export usage in JavaScript

import import 和 require 的区别 import 和js的发展历史息息相关,历史上 js没有模块(module)体系,无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来。这对开发大型工程非常不方便。在 ES6 之前,社区制定了一些模块加载方案,最主要的有 CommonJS 和 AMD 两种。前者用于服务器,后者用于浏览器。ES6 在语言标准的层面上,实现了模块功能,而且实现得相当简单,完全可以取代 CommonJS 和 AMD 规范,成为浏览器和服务器通用的模块解决方案。也就是我们常见的 require 方法。 比如 `let { stat, exists, readFile } = require(‘fs’);` 。ES6 在语言标准的层面上,实现了模块功能。ES6 模块不是对象,而是通过export命令显式指定输出的代码,再通过import命令输入。 import 的几种用法: 1. import defaultName from ‘modules.js’; 2. import { export } from ‘modules’; 3. import { export as ex1 } from […]