建造者模式在Glide源码中的应用是比较广泛的
GlideBuilder
Glide源码中最典型的建造者模式就是GlideBuilder类:
/** A builder class for setting default structural classes for Glide to use. */
@SuppressWarnings("PMD.ImmutableField")
public final class GlideBuilder {
// ……代码省略……
@NonNull
public GlideBuilder setBitmapPool(@Nullable BitmapPool bitmapPool) {
this.bitmapPool = bitmapPool;
return this;
}
@NonNull
public GlideBuilder setArrayPool(@Nullable ArrayPool arrayPool) {
this.arrayPool = arrayPool;
return this;
}
@SuppressWarnings("WeakerAccess")
@NonNull
public GlideBuilder setMemoryCache(@Nullable MemoryCache memoryCache) {
this.memoryCache = memoryCache;
return this;
}
@SuppressWarnings("WeakerAccess")
@NonNull
public GlideBuilder setDiskCache(@Nullable DiskCache.Factory diskCacheFactory) {
this.diskCacheFactory = diskCacheFactory;
return this;
}
@SuppressWarnings("WeakerAccess")
@NonNull
public GlideBuilder setSourceExecutor(@Nullable GlideExecutor service) {
this.sourceExecutor = service;
return this;
}
@SuppressWarnings("WeakerAccess")
@NonNull
public GlideBuilder setDiskCacheExecutor(@Nullable GlideExecutor service) {
this.diskCacheExecutor = service;
return this;
}
@SuppressWarnings("unused")
@NonNull
public GlideBuilder setLogLevel(int logLevel) {
if (logLevel < Log.VERBOSE || logLevel > Log.ERROR) {
throw new IllegalArgumentException(
"Log level must be one of Log.VERBOSE, Log.DEBUG," + " Log.INFO, Log.WARN, or Log.ERROR");
}
this.logLevel = logLevel;
return this;
}
// ……代码省略……
@NonNull
Glide build(
@NonNull Context context,
List<GlideModule> manifestModules,
AppGlideModule annotationGeneratedGlideModule) {
// ……代码省略……
return new Glide(
context,
engine,
memoryCache,
bitmapPool,
arrayPool,
requestManagerRetriever,
connectivityMonitorFactory,
logLevel,
defaultRequestOptionsFactory,
defaultTransitionOptions,
defaultRequestListeners,
manifestModules,
annotationGeneratedGlideModule,
experiments);
}
// ……代码省略……
}