首页 > 编程语言 >【设计模式】原型模式——其他框架源码中的原型模式

【设计模式】原型模式——其他框架源码中的原型模式

时间:2024-02-06 17:33:03浏览次数:24  
标签:Excluder return clone 源码 原型 result 设计模式 public

原型模式在其他框架源码中也有广泛的应用。


Retrofit

众所周知Retrofit是OkHttp的扩展,因此Retrofit的Call接口也像OkHttp的Call类一样实现了原型模式。Call与原型模式有关的代码如下:

public interface Call<T> extends Cloneable {
  // ……代码省略……

  /**
   * Create a new, identical call to this one which can be enqueued or executed even if this call
   * has already been.
   */
  Call<T> clone();
}

Call的子类OkHttpCall与原型模式有关的源码如下:

final class OkHttpCall<T> implements Call<T> {
  // ……代码省略……

  @SuppressWarnings("CloneDoesntCallSuperClone") // We are a final type & this saves clearing state.
  @Override
  public OkHttpCall<T> clone() {
    return new OkHttpCall<>(requestFactory, args, callFactory, responseConverter);
  }
}


Gson的Excluder类

Gson的Excluder类与原型模式有关的源码如下:

public final class Excluder implements TypeAdapterFactory, Cloneable {
  // ……代码省略……

  @Override protected Excluder clone() {
    try {
      return (Excluder) super.clone();
    } catch (CloneNotSupportedException e) {
      throw new AssertionError(e);
    }
  }

  public Excluder withVersion(double ignoreVersionsAfter) {
    Excluder result = clone();
    result.version = ignoreVersionsAfter;
    return result;
  }

  public Excluder withModifiers(int... modifiers) {
    Excluder result = clone();
    result.modifiers = 0;
    for (int modifier : modifiers) {
      result.modifiers |= modifier;
    }
    return result;
  }

  public Excluder disableInnerClassSerialization() {
    Excluder result = clone();
    result.serializeInnerClasses = false;
    return result;
  }

  public Excluder excludeFieldsWithoutExposeAnnotation() {
    Excluder result = clone();
    result.requireExpose = true;
    return result;
  }

  public Excluder withExclusionStrategy(ExclusionStrategy exclusionStrategy,
      boolean serialization, boolean deserialization) {
    Excluder result = clone();
    // ……代码省略……
    return result;
  }

  // ……代码省略……
}

可见Gson的Excluder类不但采用了原型模式,而且很多方法中,还使用clone()方法生成新的实例,非常节省资源。


Protobuf-Java

Protobuf-Java的MessageLite.Builder类与原型模式有关的代码如下:

public interface MessageLite extends MessageLiteOrBuilder {

  // ……代码省略……
  
  interface Builder extends MessageLiteOrBuilder, Cloneable {

    // ……代码省略……
    
    /**
     * Clones the Builder.
     *
     * @see Object#clone()
     */
    Builder clone();

  }
}


Glide的TransitionOptions

Glide是Android专用的框架,其他方向的读者到此可以跳过。

Glide的TransitionOptions类是用于决定你的加载完成时会发生什么的过渡选项,比如View淡入等。TransitionOptions与原型模式相关的源码如下:

public abstract class TransitionOptions<
        CHILD extends TransitionOptions<CHILD, TranscodeType>, TranscodeType>
    implements Cloneable {
    
    // ……代码省略……

	@SuppressWarnings({
    // cast to CHILD is safe given the generic argument represents the object's runtime class
    "unchecked",
    // CHILD is the correct class name.
    "PMD.CloneMethodReturnTypeMustMatchClassName",
    // we don't want to throw to be user friendly
    "PMD.CloneThrowsCloneNotSupportedException"
  	})
  	@Override
  	public final CHILD clone() {
    	try {
      		return (CHILD) super.clone();
    	} catch (CloneNotSupportedException e) {
      		throw new RuntimeException(e);
    	}
  	}

}



标签:Excluder,return,clone,源码,原型,result,设计模式,public
From: https://blog.51cto.com/dongfeng9ge/9629967

相关文章

  • 源码安装node
    进入官网https://nodejs.org/en/about/previous-releases文件拖放lrzszhttps://registry.npmmirror.com/binary.html?path=node/v12.14.1/解压sudodpkg-icode_1.49.3-1601661857_amd64.deb解压deb后缀解压7z后缀安装sudoaptinstallp7zip-full7zx文件......
  • 【Flink入门修炼】1-2 Mac 搭建 Flink 源码阅读环境
    在后面学习Flink相关知识时,会深入源码探究其实现机制。因此,需要现在本地配置好源码阅读环境。本文搭建环境:MacM1(AppleSilicon)Java8IDEAFlink官方源码一、下载Flink源码github地址:https://github.com/apache/flink考虑到一些原因,github下载可能会极其缓慢,且大......
  • 源码安装 OpenCV
    opencv_contrib额外模块,经过测试,使用成熟后才会加入到opencv:https://github.com/opencv/opencv_contrib/1.安装OpenCV所需要的依赖项目#1、安装cmakeg++wgetunzipsudoaptupdate&&sudoaptinstall-ycmakeg++wgetunzip#2、安装opencv依赖的库(通过会依赖某些图......
  • 【源码日记】了解 PLpgSQL_datum
    basedonpostgrescommitb96115acb8a0e08a46877c2b8ef2a7b5560b371bTheSQLCREATEORREPLACEFUNCTIONdemo_fors()RETURNSVOIDAS$$DECLAREaRECORD;BEGINFORaINSELECT*FROMsome_tableLOOPRAISENOTICE'id:%,name:%',a.id,a.nam......
  • 设计模式(Design Pattern)
    目录设计模式(DesignPattern)面向对象设计原则创建型模式结构型模式行为型模式设计模式(DesignPattern)概念与定义是一套被反复使用的、多数人知晓的、经过分类编目的代码设计经验的总结。设计模式(DesignPattern)是一种对于软件系统中不断重现的设计问题的解决方案进行......
  • ReentrantLock源码分析、LockSuppor、ReentrantReadWriteLock、锁优化的方法
    ReentrantLock类图我们看一下重入锁ReentrantLock类关系图,它是实现了Lock接口的类。NonfairSync和FairSync都继承自抽象类Sync,在ReentrantLock中有非公平锁NonfairSync和公平锁FairSync的实现。在重入锁ReentrantLock类关系图中,我们可以看到NonfairSync和FairSync都继承自抽象......
  • ReentrantLock源码分析、LockSuppor、ReentrantReadWriteLock、锁优化的方法
    ReentrantLock类图我们看一下重入锁ReentrantLock类关系图,它是实现了Lock接口的类。NonfairSync和FairSync都继承自抽象类Sync,在ReentrantLock中有非公平锁NonfairSync和公平锁FairSync的实现。在重入锁ReentrantLock类关系图中,我们可以看到NonfairSync和FairSync都继承自抽象......
  • 设计模式--建造者模式
    建造者模式(BuilderPattern)是一种对象构建设计模式,它提供了一种构建对象的最佳方式。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在建造者模式中,一个导演(Director)负责组织各个部分(Builder)的构建步骤,一个抽象构建器(AbstractBuilder)定义了各个部分的构建和......
  • 通达信瞄准底部源码副图
    {股票指标}VAR1:=ma(HHV(HIGH,485),17);VAR2:=MA(HHV(HIGH,222),17);VAR3:=MA(HHV(HIGH,96),17);VAR4:=MA(LLV(LOW,485),17);VAR5:=MA(LLV(LOW,222),17);VAR6:=MA(LLV(LOW,96),17);VAR7:=MA((VAR4*0.96+VAR5*0.96+VAR6*0.96+VAR1*0.558+VAR2*0.558+VAR3*0.558)/6,17);V......
  • 通达信金钱魔鬼源码副图
    {股票指标}VAR2:IF(Ema(CLOSE,5)/EMA(EMA(CLOSE,9),16)<=0.85ANDCLOSE/REF(CLOSE,1)>0.905ANDCLOSE/REF(CLOSE,1)<1.05ANDvol/CAPITAL*100<5,50,0);VAR3:=(-100)*(HHV(HIGH,34)-CLOSE)/(HHV(HIGH,34)-LLV(LOW,34))+100;VAR4:=(-100)*(HHV(HIGH,50)-CLOSE)......