【问题标题】:How to transfer variables between classes variables without creating a object second class inside first class?如何在类变量之间传输变量而不在第一类中创建对象第二类?
【发布时间】:2021-08-24 20:18:00
【问题描述】:

我有两节课。首先是 MainActivity.kt,我有一个字符串。二是这样的ServiceBuilder.kt

object ServiceBuilder {

    
    private const val URL = "https://someLink.com" // <------------- In here I should transfer the string in my MainActivity.kt class

    // Create Logger
    private val logger = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)

    // Create a Custom Interceptor to apply Headers application wide
    val headerInterceptor = object: Interceptor {

        override fun intercept(chain: Interceptor.Chain): Response {

            var request = chain.request()

            request = request.newBuilder()
                .addHeader("x-device-type", Build.DEVICE)
                .addHeader("Accept-Language", Locale.getDefault().language)
                .build()

            val response = chain.proceed(request)
            return response
        }
    }

    // Create OkHttp Client
    private val okHttp = OkHttpClient.Builder()
                                        .callTimeout(5, TimeUnit.SECONDS)
                                        .addInterceptor(headerInterceptor)
                                        .addInterceptor(logger)

    // Create Retrofit Builder
    private val builder = Retrofit.Builder().baseUrl(URL)
                                        .addConverterFactory(GsonConverterFactory.create())
                                        .client(okHttp.build())

    // Create Retrofit Instance
    private val retrofit = builder.build()

    fun <T> buildService(serviceType: Class<T>): T {
        return retrofit.create(serviceType)
    }
}

我需要正确地将字符串从 MainActivity 传输到该对象。我认为在 ServiceBuilder 中创建 MainActivity 对象并不是一个好主意,因为它分配了多余的内存。使用数据库或 SharedPreferences 也不是一个好主意,因为这种调用会减慢程序的运行速度,而这种技术是为解决其他问题而创建的。

请告诉我解决这个问题的正确方法 附言我现在这个问题很简单,但我现在不知道如何设计应用架构

【问题讨论】:

    标签: java android kotlin retrofit2


    【解决方案1】:

    是的,你是对的。将MainActivity 对象放在ServiceBuilder 中并不是一个好主意。 相反,您可以添加此类私有字段和函数以将其启动到ServiceBuilder,作为服务构建的一个步骤。然后就可以在buildService函数中使用了

    private var stringField: String? = null
    
    fun fieldString(stringField: String): ServiceBuilder {
        this.stringField = stringField
        return this
    }
    

    然后从你的活动中使用它

        ServiceBuilder
            .fieldString("string to set")
            .buildService(...)
    

    此外,您可以在此处查看有关 Kotlin 构建器模式的不同时刻 How to implement Builder pattern in Kotlin?

    【讨论】:

    • 感谢您的回复。这是很有趣的方式。但我不明白为什么它不起作用。当我创建实例 `val destinationService = ServiceBuilder .loadUrl("google.com") .buildService(DestinationService::class.java) `loadUrl("google.com") `我无法替换默认链接。 ` var url = "no-google.com" fun loadUrl(url: String): ServiceBuilder{ this.url = url return this }`
    • 请问您说“我不能替换默认链接”是什么意思?您是否在 DestinationService 中获得旧的“google.com”链接?还是您有其他问题?
    【解决方案2】:

    如果你使用类而不是对象,你可以尝试在你想要初始化它的那一刻在构造函数中传递这个值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 2017-01-22
      • 2015-04-03
      • 2019-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多