日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Mybatis插件-責任鏈模式

作者:碼籠包 更新時間: 2022-01-28 編程語言

責任鏈模式:將完整的、臃腫的接受者的實現邏輯拆分到多個只包含部分邏輯的、功能單一的Handler處理類中,開發人員可以根據業務需求將多個Handler對象組合成一條責任鏈,實現請求的處理。在一條責任鏈中,每個Handler對象都包含對下一個Handler對象的引用,一個Hanlder對象處理完請求消息(或不能處理該請求)時,會把請求傳給下一個Handler對象繼續處理,依次類推,直至整條責任鏈結束。
在這里插入圖片描述
場景:消息中有ABC三個字段,接受者HandlerA,HandlerB,HandlerC分別實現了處理三個字段的業務邏輯,當業務需要處理AC兩個字段時,開發人員可以動態組合得到HandlerA->HandlerC這條責任鏈,為了在請求消息中承載更多信息,則通過添加D字段的方式對請求消息進行升級,接收者一端可以添加HandlerD類負責處理字段D,并動態組合得到HandlerA->HanlderC->HanlderD這條責任鏈。可以動態改動責任鏈內的Handler對象的組合順序或動態新增、刪除Handler對象,滿足新的需求,可以提高系統的靈活性。
插件實際上是通過Interceptor實現的,Mybatis的插件模塊中設計責任鏈模式和JDK動態代理。

在這里插入圖片描述

/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.plugin;

import java.util.Properties;

/**
 * @author Clinton Begin
 */
public interface Interceptor {

  Object intercept(Invocation invocation) throws Throwable;

  default Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }

  default void setProperties(Properties properties) {
    // NOP
  }

}

/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.plugin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Clinton Begin
 */
public class InterceptorChain {

  private final List<Interceptor> interceptors = new ArrayList<>();

  public Object pluginAll(Object target) {
    for (Interceptor interceptor : interceptors) {
      target = interceptor.plugin(target);
    }
    return target;
  }

  public void addInterceptor(Interceptor interceptor) {
    interceptors.add(interceptor);
  }

  public List<Interceptor> getInterceptors() {
    return Collections.unmodifiableList(interceptors);
  }

}

原文鏈接:https://blog.csdn.net/Abracadabra__/article/details/112974805

欄目分類
最近更新