【Kotlin】委托模式

1 委托模式简介

        委托模式的类图结构如下。

        对应的 Kotlin 代码如下。

fun main() {
    var baseImpl = BaseImpl()
    var baseWrapper = BaseWrapper(baseImpl)
    baseWrapper.myFun1() // 打印: BaseImpl, myFun1
    baseWrapper.myFun2() // 打印: BaseImpl, myFun2
}

interface Base {
    fun myFun1()
    fun myFun2()
}

class BaseWrapper(var baseImpl: Base): Base {
    override fun myFun1() {
        baseImpl.myFun1()
    }

    override fun myFun2() {
        baseImpl.myFun2()
    }
}

class BaseImpl: Base {
    override fun myFun1() {
        println("BaseImpl, myFun1")
    }

    override fun myFun2() {
        println("BaseImpl, myFun2")
    }
}

2 类委托

        Kotlin 可以简化 BaseWrapper,简化后的代码如下。

fun main() {
    var baseImpl = BaseImpl()
    var baseWrapper = BaseWrapper(baseImpl)
    baseWrapper.myFun1() // 打印: BaseImpl, myFun1
    baseWrapper.myFun2() // 打印: BaseImpl, myFun2
}

interface Base {
    fun myFun1()
    fun myFun2()
}

class BaseWrapper(var baseImpl: Base): Base by baseImpl

class BaseImpl: Base {
    override fun myFun1() {
        println("BaseImpl, myFun1")
    }

    override fun myFun2() {
        println("BaseImpl, myFun2")
    }
}

3 属性委托

3.1 by T

import kotlin.reflect.KProperty

fun main() {
    var str: String by StringDelegate()
    str = "abc"
    println(str)
}

class StringDelegate {
    var value: String? = null

    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        println("getValue, thisRef=$thisRef, name=${property.name}, value=$value")
        return value ?: throw IllegalStateException("Property not initialized")
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
        println("setValue, thisRef=$thisRef, name=${property.name}, value=$value")
        this.value = value
    }
}

        打印日志如下。

setValue, thisRef=null, name=str, value=ABC
getValue, thisRef=null, name=str, value=ABC
ABC

3.2 by lazy

fun main() {
    // lazy为生成的委托对象, 获取属性时会执行lazy里面的操作, 只支持val变量
    val str: String by lazy { "666" }
    println(str) // 打印: 666
}

3.3 by Delegates.observable

fun main() {
    var str: String by Delegates.observable("666") { prop, old, new ->
        println("prop=$prop, old=$old, new=$new")
    }
    println(str) // 666
    // prop=property str (Kotlin reflection is not available), old=666, new=888
    str = "888"
}

3.4 by ::T

fun main() {
    var example = Example("ABC")
    example.str2 = "DEF"
    // str1=DEF, str2=DEF
    println("str1=${example.str1}, str2=${example.str2}")
}

class Example(var str1: String) {
    var str2: String by ::str1
}

        说明:属性被委托后,str1 和 str2 同时修改。

3.5 by map

fun main() {
    var map: MutableMap<String, Any> = mutableMapOf(
        "name" to "Tom",
        "age" to 23,
        "id" to 1001
    )
    var user = User(map)
    println(user) // (Tom, 23, 1001)
}

class User(map: MutableMap<String, Any>) {
    var name: String by map
    var age: Int by map
    var id: Int by map

    override fun toString(): String = "($name, $age, $id)"
}

相关推荐

  1. Kotlin 委托

    2024-04-03 10:00:01       14 阅读
  2. Kotlin中的委托

    2024-04-03 10:00:01       41 阅读
  3. Kotlin(十七) 泛型和委托

    2024-04-03 10:00:01       39 阅读
  4. Android(Kotlin) 委托(by) 封装 SharedPreferences

    2024-04-03 10:00:01       69 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-03 10:00:01       5 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-03 10:00:01       5 阅读
  3. 在Django里面运行非项目文件

    2024-04-03 10:00:01       4 阅读
  4. Python语言-面向对象

    2024-04-03 10:00:01       6 阅读

热门阅读

  1. pytorch | yolov5 Can not get arrribute SiLU

    2024-04-03 10:00:01       21 阅读
  2. 常见哈希算法及其应用场景

    2024-04-03 10:00:01       31 阅读
  3. 设计模式(14):命令模式

    2024-04-03 10:00:01       23 阅读
  4. 1344: 【递推】【入门】流感传染

    2024-04-03 10:00:01       19 阅读
  5. WebKit内核架构深度解析:核心技术与工作机制

    2024-04-03 10:00:01       21 阅读
  6. web有哪些方式可以实时更新数据

    2024-04-03 10:00:01       27 阅读
  7. vue 基础回顾

    2024-04-03 10:00:01       23 阅读
  8. 【2024最新】vue3的基本使用(超详细)

    2024-04-03 10:00:01       20 阅读
  9. freertosday3

    2024-04-03 10:00:01       24 阅读
  10. 平台介绍-大屏组件

    2024-04-03 10:00:01       21 阅读
  11. Facebook推广常见问题解惑答疑

    2024-04-03 10:00:01       22 阅读
  12. SegmentAnything导出Onnx模型分割图片

    2024-04-03 10:00:01       21 阅读
  13. 120.单例模式(C++设计模式)

    2024-04-03 10:00:01       19 阅读