Java设计模式 适配器模式

适配器模式

案例:在日常开发中,我们很多时候需要序列化和反序列化传输,序列化的框架有很多,如fastjson,kryo,hessian,probobuf,以及最近新出来的fury。在许多开源项目中都会适配很多种序列化方式

先不说概念,先上代码,通过两种序列化框架来模拟 序列化框架的选择

引入依赖

<dependency>
    <groupId>com.esotericsoftware</groupId>
    <artifactId>kryo</artifactId>
    <version>5.5.0</version>
</dependency>

<dependency>
    <groupId>org.furyio</groupId>
    <artifactId>fury-core</artifactId>
    <version>0.1.0</version>
</dependency>
<dependency>
    <groupId>org.furyio</groupId>
    <artifactId>fury-format</artifactId>
    <version>0.1.0</version>
</dependency>

定义一个序列化接口 MySerializer

package com.example.spring.boot.test.adaptermode;

/**
 * @Description
 * @Author 
 * @Date 2023/9/21
 */
public interface MySerializer {

    /**
     * 序列化
     * @param t
     * @return
     */
    <T> byte[] serialize(T t);

    /**
     * 反序列化
     * @param bytes
     * @return
     */
    <T> T deserialize(byte[] bytes);

}

定义适配器 FurySerializerAdapter

package com.example.spring.boot.test.adaptermode;

import com.example.spring.boot.test.fury.FuryEntity;
import io.fury.Fury;
import io.fury.Language;

public class FurySerializerAdapter implements MySerializer{
		/**
     * 模拟go 提供的接口
     */
    private Fury fury;

    public FurySerializerAdapter(Class<?> cls){
        fury = Fury.builder().withLanguage(Language.JAVA).build();
        fury.register(cls);
    }


    @Override
    public <T> byte[] serialize(T t) {
        return fury.serialize(t);
    }

    @Override
    public <T> T deserialize(byte[] bytes) {
        return (T) fury.deserialize(bytes);
    }
}

创建适配器 KryoSerializerAdapter

package com.example.spring.boot.test.adaptermode;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;


public class KryoSerializerAdapter implements MySerializer{

    private Kryo kryo;

    public KryoSerializerAdapter(){
        kryo = new Kryo();
        kryo.setReferences(true);
        kryo.setRegistrationRequired(false);
    }

    @Override
    public <T> byte[] serialize(T t) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Output output = new Output(byteArrayOutputStream);
        kryo.writeClassAndObject(output, t);
        output.close();
        return byteArrayOutputStream.toByteArray();
    }

    @Override
    public <T> T deserialize(byte[] bytes) {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        Input input = new Input(byteArrayInputStream);
        input.close();
        return (T) kryo.readClassAndObject(input);
    }

}

创建一个测试实体类

@Data
public class FuryEntity {

    private Integer age;

    private String name;

    private Date birthday;

    private Double money;

}

测试:

package com.example.spring.boot.test.adaptermode;

import com.example.spring.boot.test.fury.FuryEntity;

import java.util.Base64;
import java.util.Date;

public class AdapterTest {
    public static void main(String[] args) {
        FuryEntity entity = new FuryEntity();
        entity.setAge(18);
        entity.setName("乔峰");
        entity.setBirthday(new Date());
				
      	// 使用fury序列化
        MySerializer furySerializer = new FurySerializerAdapter(FuryEntity.class);

        byte[] furyByte = furySerializer.serialize(entity);
        String encodedFuryByte = Base64.getEncoder().encodeToString(furyByte);
        System.out.println(encodedFuryByte);

        System.out.println(furySerializer.deserialize(furyByte).toString());
        // 使用kryo序列化
        MySerializer kyroSerializer = new KryoSerializerAdapter();
        byte[] kryoByte = kyroSerializer.serialize(entity);
        String encodedKryoByte = Base64.getEncoder().encodeToString(kryoByte);
        System.out.println(encodedKryoByte);

        System.out.println(kyroSerializer.deserialize(kryoByte).toString());
    }
}

结果:

Av8BiQBGaFnY/QASAAAAAAEEVE7wXAABMQBaLrW2igEAAA==
FuryEntity(age=18, name=乔峰, birthday=Thu Sep 21 15:47:21 CST 2023, money=null)

AQBjb20uZXhhbXBsZS5zcHJpbmcuYm9vdC50ZXN0LmZ1cnkuRnVyeUVudGl0+QEBJAEBamF2YS51dGlsLkRhdOUB1MnltasxAAGD5LmU5bOw
FuryEntity(age=18, name=乔峰, birthday=Thu Sep 21 15:51:57 CST 2023, money=null)

适配器定义

适配器模式主要目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。生活中常见的转接头就是这个原理。

网上举得最多的例子就是电源适配器的案例可以加深理解

适配器在Mybatis框架中的运用

定义了一个 Log 接口

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.apache.ibatis.logging;

public interface Log {
    boolean isDebugEnabled();

    boolean isTraceEnabled();

    void error(String var1, Throwable var2);

    void error(String var1);

    void debug(String var1);

    void trace(String var1);

    void warn(String var1);
}

log4j的适配器

/**
 *    Copyright 2009-2017 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.logging.log4j2;

import org.apache.ibatis.logging.Log;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.spi.AbstractLogger;

/**
 * @author Eduardo Macarron
 */
public class Log4j2Impl implements Log {

  private final Log log;

  public Log4j2Impl(String clazz) {
    Logger logger = LogManager.getLogger(clazz);

    if (logger instanceof AbstractLogger) {
      log = new Log4j2AbstractLoggerImpl((AbstractLogger) logger);
    } else {
      log = new Log4j2LoggerImpl(logger);
    }
  }

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

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

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

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

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

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

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

}

slf4j的适配器

/**
 *    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.logging.slf4j;

import org.apache.ibatis.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.spi.LocationAwareLogger;

/**
 * @author Clinton Begin
 * @author Eduardo Macarron
 */
public class Slf4jImpl implements Log {

  private Log log;

  public Slf4jImpl(String clazz) {
    Logger logger = LoggerFactory.getLogger(clazz);

    if (logger instanceof LocationAwareLogger) {
      try {
        // check for slf4j >= 1.6 method signature
        logger.getClass().getMethod("log", Marker.class, String.class, int.class, String.class, Object[].class, Throwable.class);
        log = new Slf4jLocationAwareLoggerImpl((LocationAwareLogger) logger);
        return;
      } catch (SecurityException | NoSuchMethodException e) {
        // fail-back to Slf4jLoggerImpl
      }
    }

    // Logger is not LocationAwareLogger or slf4j version < 1.6
    log = new Slf4jLoggerImpl(logger);
  }

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

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

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

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

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

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

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

}

通过 LogFactory 选择适配

/**
 *    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.logging;

import java.lang.reflect.Constructor;

/**
 * @author Clinton Begin
 * @author Eduardo Macarron
 */
public final class LogFactory {

  /**
   * Marker to be used by logging implementations that support markers.
   */
  public static final String MARKER = "MYBATIS";

  private static Constructor<? extends Log> logConstructor;

  static {
    tryImplementation(LogFactory::useSlf4jLogging);
    tryImplementation(LogFactory::useCommonsLogging);
    tryImplementation(LogFactory::useLog4J2Logging);
    tryImplementation(LogFactory::useLog4JLogging);
    tryImplementation(LogFactory::useJdkLogging);
    tryImplementation(LogFactory::useNoLogging);
  }

  private LogFactory() {
    // disable construction
  }

  public static Log getLog(Class<?> aClass) {
    return getLog(aClass.getName());
  }

  public static Log getLog(String logger) {
    try {
      return logConstructor.newInstance(logger);
    } catch (Throwable t) {
      throw new LogException("Error creating logger for logger " + logger + ".  Cause: " + t, t);
    }
  }

  public static synchronized void useCustomLogging(Class<? extends Log> clazz) {
    setImplementation(clazz);
  }

  public static synchronized void useSlf4jLogging() {
    setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
  }

  public static synchronized void useCommonsLogging() {
    setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);
  }

  public static synchronized void useLog4JLogging() {
    setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
  }

  public static synchronized void useLog4J2Logging() {
    setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
  }

  public static synchronized void useJdkLogging() {
    setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
  }

  public static synchronized void useStdOutLogging() {
    setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);
  }

  public static synchronized void useNoLogging() {
    setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
  }

  private static void tryImplementation(Runnable runnable) {
    if (logConstructor == null) {
      try {
        runnable.run();
      } catch (Throwable t) {
        // ignore
      }
    }
  }

  private static void setImplementation(Class<? extends Log> implClass) {
    try {
      Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
      Log log = candidate.newInstance(LogFactory.class.getName());
      if (log.isDebugEnabled()) {
        log.debug("Logging initialized using '" + implClass + "' adapter.");
      }
      logConstructor = candidate;
    } catch (Throwable t) {
      throw new LogException("Error setting Log implementation.  Cause: " + t, t);
    }
  }

}
0%