美文网首页
Convert转换模式

Convert转换模式

作者: 石器时代小古董 | 来源:发表于2018-02-05 16:31 被阅读0次
1.数据类型进行转换时,如果客户端不需要了解转换的细节,可以用转换器作为中间件。
2.当你可能需要转换成不同数据而不期望改动实现逻辑。可以通过传递不同的转换器实现。
3.从数据库中获得数据转换成对应的Bean.

参考Retrofit的转换接口

public interface Converter<F, T> {
 //转换接口
  T convert(F value) throws IOException;
  //指定生成转换器接口
  abstract class Factory {
    
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
        Retrofit retrofit) {
      return null;
    }

    public Converter<?, RequestBody> requestBodyConverter(Type type,
        Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
      return null;
    }

    public Converter<?, String> stringConverter(Type type, Annotation[] annotations,
        Retrofit retrofit) {
      return null;
    }
  }
}

默认转换类

image.png

扩展的转换类

image.png

客户端使用

可以看到,他们都将传入的位置类型的数据转换成一个RequestBody类型供客户端使用。而客户端并不知道具体的转换细节。

    /**
     * Builds a method return value from an HTTP response body.
     */
    T toResponse(ResponseBody body) throws IOException {
        return responseConverter.convert(body);
    }

如果客户端需要替换一个生成RequestBody的方式,只需要在替换一下实现的方式


image.png

相关文章

  • Convert转换模式

    参考Retrofit的转换接口 默认转换类 扩展的转换类 客户端使用 可以看到,他们都将传入的位置类型的数据转换成...

  • Linux中gif的制作和图片转换

    图片转换命令convert convert 命令是由 ImageMagick 包提供。 选择转换为jpg图片会比转...

  • SQL Server 时间相关的内容

    convert转换日期格式: 格式:CONVERT(data_type(length),data_to_be_co...

  • SQL Server常用函数

    1. CONVERT() 函数 CONVERT() 函数是把日期转换为新数据类型的通用函数。CONVERT() 函...

  • 常用日期函数

    GetDate() 返回系统当前时间 Convert() 数据类型转换,常用来格式化日期格式 CONVERT(...

  • 数据类型转换和绝对值

    SQL进行数据类型转换 1. convert(数据类型, 字段名) convert(datetime, start...

  • ffmpeg 音频转换

    音频转换主要APIswr_alloc_set_opts 设置转换的参数 swr_convert 在上面转换参数设...

  • iOS中一些共存混编的问题

    MRC代码转换成ARC代码Xcode --> Edit --> Refactor --> Convert to ...

  • 常用工具网站收藏

    djvu 转 pdf Smallpdf convert anything to anything 在线格式转换 在...

  • Weekly 2021-10

    MySQL 一、日期与时间 1.1 convert_tz convert_tz 能转换成你想要的时区,就将默认时区...

网友评论

      本文标题:Convert转换模式

      本文链接:https://www.haomeiwen.com/subject/kbhizxtx.html