工厂模式--以Glide.with为例

1、工厂模式的作用,创建对象,为啥不直接用new 而用工厂模式

原因,1、初始化复杂,耗时,将对象交给工厂来生产,实现高度内聚;2、省略了繁杂的判断条件

 

抽象模式解释非常到位:

https://blog.youkuaiyun.com/qq_33905217/article/details/109029517?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162114305016780271563578%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=162114305016780271563578&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-109029517.first_rank_v2_pc_rank_v29&utm_term=%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F&spm=1018.2226.3001.4187

 

Glide.with(Context context)

前面说到,Glide 生命周期的时候,说过Context 与fragment 绑定,requestManager 与 fragment 绑定实现了生命周期管理。

现在看下如何实现根据不同的context 去拿到requestManger,对用户来说仅需要知道把context 放入即可,工厂类内部根据传入的context 创建与之对应的生命周期。

Glide.class:以传入activity 为例

public static RequestManager with(Context context) {
//单例模式创建工程实例
    RequestManagerRetriever retriever = RequestManagerRetriever.get();
    return retriever.get(context);
}

public static RequestManager with(Activity activity) {
    RequestManagerRetriever retriever = RequestManagerRetriever.get();
    return retriever.get(activity);
}

public static RequestManager with(FragmentActivity activity) {
    RequestManagerRetriever retriever = RequestManagerRetriever.get();
    return retriever.get(activity);
}

@TargetApi(11)
public static RequestManager with(Fragment fragment) {
    RequestManagerRetriever retriever = RequestManagerRetriever.get();
    return retriever.get(fragment);
}

public static RequestManager with(android.support.v4.app.Fragment fragment) {
    RequestManagerRetriever retriever = RequestManagerRetriever.get();
    return retriever.get(fragment);
}

看下工厂类:RequestManagerRetriever

public class RequestManagerRetriever implements Callback {
private static final RequestManagerRetriever INSTANCE = new RequestManagerRetriever();
public static RequestManagerRetriever get() {
    return INSTANCE;
}
public RequestManager get(Context context) {
    if (context == null) {
        throw new IllegalArgumentException("You cannot start a load on a null Context");
    } else {
        if (Util.isOnMainThread() && !(context instanceof Application)) {
            if (context instanceof FragmentActivity) {
                return this.get((FragmentActivity)context);
            }

            if (context instanceof Activity) {
                return this.get((Activity)context);
            }

            if (context instanceof ContextWrapper) {
                return this.get(((ContextWrapper)context).getBaseContext());
            }
        }

        return this.getApplicationManager(context);
    }
}
......
@TargetApi(11)
public RequestManager get(Activity activity) {
    if (!Util.isOnBackgroundThread() && VERSION.SDK_INT >= 11) {
        assertNotDestroyed(activity);
        FragmentManager fm = activity.getFragmentManager();
        return this.fragmentGet(activity, fm);
    } else {
        return this.get(activity.getApplicationContext());
    }
}
......
@TargetApi(11)
RequestManager fragmentGet(Context context, FragmentManager fm) {
    RequestManagerFragment current = this.getRequestManagerFragment(fm);
    RequestManager requestManager = current.getRequestManager();
    if (requestManager == null) {
        requestManager = new RequestManager(context, current.getLifecycle(), current.getRequestManagerTreeNode());
        current.setRequestManager(requestManager);
    }

    return requestManager;
}

根据context 创建的fragment 的TAG 判断requestManger 是否存在

@TargetApi(17)
RequestManagerFragment getRequestManagerFragment(FragmentManager fm) {
    RequestManagerFragment current = (RequestManagerFragment)fm.findFragmentByTag("com.bumptech.glide.manager");
    if (current == null) {
        current = (RequestManagerFragment)this.pendingRequestManagerFragments.get(fm);
        if (current == null) {
            current = new RequestManagerFragment();
            this.pendingRequestManagerFragments.put(fm, current);
            fm.beginTransaction().add(current, "com.bumptech.glide.manager").commitAllowingStateLoss();
            this.handler.obtainMessage(1, fm).sendToTarget();
        }
    }

    return current;
}

 

}

 

 

 

jiejiatao@jiejiataodeMacBook-Pro wuyang_android % ./gradlew :module_common:dependencies --configuration api > Configure project :app EcLicensePlugin :当前工程的gradle版本为: 7.5 app添加编译模块:module_aar app添加编译模块:module_account app添加编译模块:module_car_service app添加编译模块:module_carInfo app添加编译模块:module_equipment app添加编译模块:module_main app添加编译模块:module_mall app添加编译模块:module_showroom app添加编译模块:module_thirdPlatform app添加编译模块:module_web app添加编译模块:resource EcLicensePlugin :packageName: null EcLicensePlugin :variant: formalDebug appPackageName: null flavorPackageName: com.whmapp.wuyang.honda EcLicensePlugin :variant: uatDebug appPackageName: null flavorPackageName: com.whmappcs.wuyang.honda EcLicensePlugin :variant: developDebug appPackageName: null flavorPackageName: com.whmappcs.wuyang.honda EcLicensePlugin :variant: formalRelease appPackageName: null flavorPackageName: com.whmapp.wuyang.honda EcLicensePlugin :variant: uatRelease appPackageName: null flavorPackageName: com.whmappcs.wuyang.honda EcLicensePlugin :variant: developRelease appPackageName: null flavorPackageName: com.whmappcs.wuyang.honda --W- The variant: formalDebug, There's no json file --W- The variant: uatDebug, There's no json file --W- The variant: developDebug, There's no json file --W- The variant: formalRelease, There's no json file --W- The variant: uatRelease, There's no json file --W- The variant: developRelease, There's no json file > Configure project :module_common module_common: 'annotationProcessor' dependencies won't be recognized as kapt annotation processors. Please change the configuration name to 'kapt' for these artifacts: 'com.github.bumptech.glide:compiler:4.9.0'. > Task :module_common:dependencies ------------------------------------------------------------ Project ':module_common' ------------------------------------------------------------ api - API dependencies for 'main' sources. (n) +--- unspecified (n) +--- unspecified (n) +--- androidx.databinding:viewbinding:8.3.2 (n) +--- com.blankj:utilcodex:1.31.1 (n) +--- org.jetbrains.kotlin:kotlin-stdlib:1.7.10 (n) +--- org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0 (n) +--- androidx.core:core-ktx:1.6.0 (n) +--- com.squareup.okhttp3:okhttp:4.9.0 (n) +--- com.squareup.okhttp3:logging-interceptor:4.9.0 (n) +--- io.reactivex.rxjava2:rxjava:2.2.19 (n) +--- com.squareup.retrofit2:retrofit:2.9.0 (n) +--- io.reactivex.rxjava2:rxandroid:2.1.0 (n) +--- com.squareup.retrofit2:converter-gson:2.9.0 (n) +--- com.squareup.retrofit2:adapter-rxjava2:2.4.0 (n) +--- com.google.code.gson:gson:2.10.1 (n) +--- org.greenrobot:eventbus:3.2.0 (n) +--- com.geyifeng.immersionbar:immersionbar:3.2.2 (n) +--- com.guolindev.permissionx:permissionx:1.6.0 (n) +--- com.github.li-xiaojun:XPopup:2.10.0 (n) +--- com.github.bumptech.glide:glide:4.14.2 (n) +--- jp.wasabeef:glide-transformations:4.3.0 (n) +--- com.github.zjupure:webpdecoder:2.0.4.12.0 (n) +--- io.github.scwang90:refresh-layout-kernel:2.1.0 (n) +--- io.github.scwang90:refresh-header-classics:2.1.0 (n) +--- io.github.scwang90:refresh-footer-classics:2.1.0 (n) +--- com.github.JessYanCoding:AndroidAutoSize:v1.2.1 (n) +--- com.android.support:appcompat-v7:27.+ (n) +--- com.android.support:design:27.+ (n) +--- com.orhanobut:hawk:2.0.1 (n) +--- com.baidu.lbsyun:baidu-lbs-aar:1.5 (n) +--- com.baidu.lbsyun:java-poet:1.1 (n) +--- com.baidu.lbsyun:onsdk_all:1.7.1 (n) +--- com.baidu.lbsyun:protobuf_gens-map:1.4 (n) +--- cn.jiguang.sdk:jpush:5.7.0 (n) +--- com.huawei.hms:push:6.13.0.300 (n) +--- cn.jiguang.sdk.plugin:huawei:5.7.0 (n) +--- cn.jiguang.sdk.plugin:vivo:5.7.0 (n) +--- cn.jiguang.sdk.plugin:xiaomi:5.7.0 (n) +--- cn.jiguang.sdk.plugin:oppo:5.7.0 (n) +--- commons-codec:commons-codec:1.6 (n) +--- androidx.annotation:annotation:1.1.0 (n) +--- cn.jiguang.sdk.plugin:honor:5.7.0 (n) +--- cn.jiguang.sdk:jverification:3.2.5 (n) +--- com.tencent.bugly:crashreport:latest.release (n) +--- io.github.idisfkj:android-startup:1.1.0 (n) +--- com.auth0:java-jwt:4.4.0 (n) +--- io.jsonwebtoken:jjwt:0.9.1 (n) +--- com.github.lzyzsd:jsbridge:1.0.4 (n) +--- com.google.android.material:material:1.6.0 (n) +--- unspecified (n) +--- com.github.yyued:SVGAPlayer-Android:2.6.1 (n) +--- unspecified (n) +--- com.google.android.exoplayer:exoplayer:2.18.1 (n) +--- io.github.youth5201314:banner:2.2.3 (n) +--- com.github.zhpanvip:bannerviewpager:3.5.12 (n) +--- com.github.getActivity:XXPermissions:20.0 (n) +--- com.airbnb.android:lottie:5.2.0 (n) +--- com.huawei.hms:scanplus:2.12.0.300 (n) +--- net.easyconn:ec:12.2.1.0415 (n) +--- net.easyconn:common:12.2.1.0415 (n) +--- net.easyconn:carmanble:12.2.1.0415 (n) +--- net.easyconn:rotation:12.2.1.0415 (n) +--- net.easyconn:speech_iflytek:12.2.1.0415 (n) +--- net.easyconn:theme_main:+ (n) +--- net.easyconn:carman_baidumap:0.6.16.2 (n) +--- com.huawei.hms:scanplus:2.8.0.300 (n) +--- com.github.hackware1993:MagicIndicator:1.6.0 (n) +--- com.github.hackware1993:MagicIndicator:1.7.0 (n) +--- project module_aar (n) +--- project resource (n) +--- com.ligo:LogRecord:1.0.5 (n) +--- com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.8.4 (n) \--- androidx.databinding:viewbinding:7.1.0 (n) (n) - Not resolved (configuration is not meant to be resolved) A web-based, searchable dependency report is available by adding the --scan option. Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. See https://docs.gradle.org/7.5/userguide/command_line_interface.html#sec:command_line_warnings BUILD SUCCESSFUL in 686ms 1 actionable task: 1 executed 只列出有哪些依赖,但是无法出现依赖的大小
最新发布
06-26
Gradle 构建输出默认情况下不会显示模块依赖项的大小信息,包括 `module_common` 模块。这是由于 Gradle 的依赖管理机制主要关注版本、传递性以及冲突解决,而非依赖项的物理大小 [^1]。 ### Gradle 默认行为 Gradle 的构建输出通常会列出项目中所有模块的依赖关系树(通过命令 `./gradlew dependencies`),但并不会包含每个依赖项的文件大小。这种设计是为了优化构建性能和简化依赖分析过程 [^1]。 ### 获取依赖项大小的方法 如果需要查看依赖项的大小,可以使用以下方法: #### 1. 使用 `dependency-tree` 插件 某些第三方插件(如 `dependency-tree`)可以生成更详细的依赖信息,包括文件大小。可以通过在 `build.gradle` 文件中添加相关配置来启用此类插件。 #### 2. 手动查找 JAR 文件大小 - **Maven 本地仓库**:Gradle 缓存依赖项的位置通常位于 `~/.gradle/caches/` 或项目级 `.gradle` 目录中。可以在这些目录下找到具体的依赖项文件(JAR 或 AAR),然后通过文件系统查看其大小。 - **Gradle 缓存路径示**: ``` ~/.gradle/caches/modules-2/files-2.1/ ``` #### 3. 自定义 Gradle 任务 可以通过编写自定义 Gradle 脚本,遍历依赖项并计算其大小。如: ```groovy task showDependencySizes { doLast { configurations.compile.files.each { File file -> println "Dependency: ${file.name}, Size: ${file.length() / 1024} KB" } } } ``` 运行此任务后,Gradle 将输出每个依赖项的名称及其大小 [^1]。 ### 注意事项 - Gradle 的依赖管理旨在提供高效的构建流程,因此依赖项大小信息通常不是必需的。只有在特定场景(如优化 APK 大小或进行依赖清理时)才需要此类信息 [^1]。 - 如果项目使用了远程仓库(如 Maven Central 或 JCenter),依赖项的大小可能还受到网络传输和缓存策略的影响 [^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值