Glide 设计模式之单例模式
单例定义
属于创建型模式;全局只创建⼀个对象。
Glide 使⽤形式
中科大校长Glide 单例写法
context检查
<(context)单例开始Glide .with (context )。。。
1/** * @e #with(android.app.Activity) * @e #with(android.app.Fragment) * @e #with(androidx.fragment.app.Fragment) * @e #with(androidx.fragment.app.FragmentActivity) */ @NonNull public static RequestManager with (@NonNull Context context ) { return getRetriever (context ).get (context ); } 。。。。。。@NonNull private static RequestManagerRetriever getRetriever (@Nullable Context context ) { // Context could be null for other reasons (ie the ur pass in null), but in practice it will // only occur due to errors with the Fragment lifecycle. Precondi
银瓶嘴tions .checkNotNull ( context , "You cannot start a load on a not yet attached View or a Fragment where getActivity() " + "returns null (which usually occurs when getActivity() is called before the Fragment " + "is attached or after the Fragment is destroyed)."); return Glide .get (context ).getRequestManagerRetriever (); }
1香亭
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
volatile:在只有DCL没有volatile的懒加载单例模式中,仍然存在着并发陷阱。感兴趣的可以看看参考《》或者搜索“volatile 单例模式”相信⾥⾯的并发和内存模型的原理会让你⼤呼精彩 @GuardedBy("Glide.class") private static volatile Glide glide ;/** * Get the singleton. * * @return the singleton */ @NonNull // Double checked locking is safe here. @SuppressWarnings("GuardedBy") public static Glide get (@NonNull Context context ) { if (glide == null ) { GeneratedAppGlideModule annotationGeneratedModule = getAnnotationGeneratedGlideModules (context .getApplicationContext ()); synchronized (Glide .class ) { if (glide == null ) { checkAndInitializeGlide (context , annotationGeneratedModule ); } } } return glide ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25@Nullable @SuppressWarnings({"unchecked", "TryWithIdenticalCatches", "PMD.UnudFormalParameter"}) private static GeneratedAppGlideModule getAnnotationGeneratedGlideModules (Context context ) { GeneratedAppGlideModule result = null ; try { Class <GeneratedAppGlideModule > clazz = (Class <GeneratedAppGlideModule >) Class .forName ("com.bumptech.glide.GeneratedAppGlideModuleImpl"); result = clazz .getDeclaredConstructor (Context .class ).newInstance (context .getApplicationContext ()); } catch (ClassNotFoundException e ) { if (Log .isLoggable (TAG , Log .WARN )) { Log .w ( TAG , "Failed to find GeneratedAppGlideModule. You should include an" + " annotationProcessor compile dependency on com.github.bumptech.glide:compiler" + " in your application and a @GlideModule annotated AppGlideModule implementation" + " or LibraryGlideModules will be silently ignored"); } // The exceptions can't be squashed across all versions of Android. } catch (InstantiationException e ) { throwIncorrectGlideModule (e ); } catch (IllegalAccessException e ) { throwIncorrectGlideModule (e ); } catch (NoSuchMethodException e ) { throwIncorrectGlideModule (e ); } catch (InvocationTargetException e ) { throwIncorrectGlideModule (e ); } return result ; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
爆笑图片20
21
22
23
24
25
26
27
28
29
30
31
初次看到的GeneratedAppGlideModule annotationGeneratedModule =
ApplicationContext());
有些诧异,在之前的旧版本⾥是没有的,看到源码的⽇志:
很明显是来验证是否添加依赖和注解
依赖例⼦如下:
注解例⼦如下:
所以到这可以看出这是进⾏使⽤环境搭建检查。为后续单例对象建⽴护航。
单例对象建⽴
初始化标识检查,接着往下:"Failed to find GeneratedAppGlideModule. You should include an" + " annotationProcessor compile dependency on com.github.bumptech.glide:compiler" + " in your application and a @GlideModule annotated AppGlideModule implementation" + " or LibraryGlideModules will be silently ignored"
1
2
3
4annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
1@GlideModule public final class MyAppGlideModule extends AppGlideModule {}
1
2 @GuardedBy("Glide.class") private static void checkAndInitializeGlide ( @NonNull Context context , @Nullable GeneratedAppGlideModule generatedAppGlideModule ) { // In the thread running initGlide(), one or more class may (context). // Without this check, tho calls could trigger infinite recursion. if (isInitializing ) { throw new IllegalStateException ( "You cannot () in registerComponents()," + " u the provided Glide instance instead"); } isInitializing = true ; initializeGlide (context , generatedAppGlideModule ); isInitializing = fal ; }
1
2
3
4
5
6
7干锅土豆片的做法
8
9
10
11
12
13
14@GuardedBy("Glide.class") private static void initializeGlide ( @NonNull Context context ,
@Nullable GeneratedAppGlideModule generatedAppGlideModule ) { initializeGlide (context , new GlideBuilder (), generatedAppGlideModule ); }
1
2
3
4
5@GuardedBy("Glide.class") @SuppressWarnings("deprecation") private static void initializeGlide ( @NonNull Context context , @NonNull GlideBuilder builder , @Nullable GeneratedAppGlideModule annotationGeneratedModule ) { Context applicationContext = context .getApplicationContext (); //创建空集合对象。避开NullPointObject 【建议列表初始化空对象列表】详情参考“附录1” List <GlideModule > manifestModules = Collections .emptyList ();
// if (annotationGeneratedModule == null || annotationGeneratedModule .isManifestParsingEnabled ()) { manifestModules = new ManifestParr (applicationContext ).par (); }//Glide Generated API 可在 Application 和 Library 中被扩展。扩展使⽤被注解的静态⽅
法来添加新的选项、修改现有选项、甚⾄添加额外的类型⽀持1
计算机怎么关机2
3
4
5
6
7
8
9
10
11
12
13
14
15
Log.isLoggable(TAG, Log.DEBUG
(GlideModule glideModule Log.d(TAG,"Discovered GlideModule from manifest: "
创建新的选项,通常与 Glide 的 Option
防火教案
(GlideModule
module.applyOptions(applicationContext
注册⾃定义 manifestModules
(GlideModule
try{
嗯!这个单例对象的建⽴⽐较复杂了,从上到下⼤致作⽤如下:
1. 集成库可以为 Generated API 扩展⾃定义选项。
2. 在 Application 模块中可将常⽤的选项组打包成⼀个选项在Generated API 中使⽤
3. GlideOption - 为 RequestOptions 添加⼀个⾃定义的选项。
4. GlideType - 添加对新的资源类型的⽀持(GIF,SVG 等等)。
annotationGeneratedModule ⽣效判断以及应⽤设置
清单解析
为了简化迁移过程,尽管清单解析和旧的 GlideModule 接⼝已被废弃,但它们在 v4 版本中仍被⽀持。
AppGlideModule,LibraryGlideModule,与已废弃的 GlideModule 可以在⼀个应⽤中共存。
然⽽,为了避免检查元数据的性能天花板(以及相关的 bugs ),你可以在迁移完成后禁⽤掉清单解析,在你的 AppGlideModule 中复写⼀个⽅法:
清单解析不是本次单例模式解析重点。因此暂不展开说明ManifestParr
Glide 具体参数构建过程如下
com.bumptech.glide.GlideBuilder if (annotationGeneratedModule == null || annotationGeneratedModule .isManifestParsingEnabled ()) { manifestModules = new ManifestParr (applicationContext ).par (); }
1
2
3@GlideModule public class GiphyGlideModule extends AppGlideModule { @Override public boolean isManifestParsingEnabled () { return fal ; } ...}
1
2
3
4
5
6
7
中核华兴oa8
9private final Map <Class <?>, TransitionOptions <?, ?>> defaultTransitionOptions = new ArrayMap <>(); private final GlideExperiments .Builder glideExperimentsBuilder = new GlideExperiments .Builder (); private Engine engine ; private BitmapPool bitmapPool ; private ArrayPool arrayPool ; private MemoryCache memoryCache ; private GlideExecutor sourceExecutor ; private GlideExecutor diskCacheExecutor ; private DiskCache .Factory diskCacheFactory ; private MemorySizeCalculator memorySizeCalculator ; private ConnectivityMonitorFactory connectivityMonitorFactory ; private int logLevel = Log .INFO ; private RequestOptionsFactory defaultRequestOptionsFactory = new RequestOptionsFactory () { @
NonNull @Override public RequestOptions build () { return new RequestOptions (); } }; @Nullable private RequestManagerFactory requestManagerFactory ; private GlideExecutor animationExecutor ; private boolean isActiveResourceRetentionAllowed ; @Nullable private List <RequestListener <Object >> defaultRequestListeners ;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24