【问题标题】:Is there a way to lazy-init a costly field in a specific constructor?有没有办法在特定的构造函数中延迟初始化一个昂贵的字段?
【发布时间】:2020-11-24 19:28:19
【问题描述】:

我有一个要填充的数据类,在一个构造函数中我已经有了数据,而在另一个构造函数中我只想在需要时获取它,这很少见。

示例代码如下:

data class Source1(val str1: String)
data class Source2(val str2: String)

data class DTO(val data1: String, val data2: String) {
    // ctor which does not need laziness
    constructor(source1: Source1) : this(
        data1 = source1.str1,
        data2 = source1.str1
    )

    // ctor which needs costly data
    constructor(source2: Source2, costlyData: String) : this(
        data1 = source2.str2,
        data2 = costlyData
    )
}

fun demo() {
    val source1 = Source1("some str - 1")
    DTO(source1)

    val source2 = Source2("some str - 2")
    val costlyData: String = costlyOperation() // this is the operation I'd like to execute lazily
    DTO(source2, costlyData)
}

【问题讨论】:

标签: kotlin lazy-loading lazy-initialization


【解决方案1】:

我想说最简单的方法是接受一个函数作为构造函数参数,如下所示:

class DTO(provider:()->String){
    constructor(data: String):this({data})

    val data by lazy{ provider()}
}

所以你可以两种方式使用它:

val eager = DTO("some str - 1")
val lazy = DTO(::costlyOperation)

更好的方法是使用具有不同实现的Source 抽象来提供常量值并执行操作。但总体思路是一样的。

虽然我不会再调用这个 DTO 并且它失去了关于内容的数据类功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2018-11-01
    相关资源
    最近更新 更多