一、桥接相关类
package com.study.bridge
abstract class Identity {
abstract fun getIdentity(): String
}
package com.study.bridge
class BossIdentity : Identity() {
override fun getIdentity(): String = "老板"
}
package com.study.bridge
class StudentIdentity : Identity() {
override fun getIdentity(): String = "学生"
}
package com.study.bridge
abstract class Phone(var identity: Identity) {
abstract fun usePhone()
}
package com.study.bridge
class FunctionPhone(identity: Identity) : Phone(identity) {
override fun usePhone() {
println("${identity.getIdentity()}相机类型手机")
}
}
package com.study.bridge
class CameraPhone(identity: Identity) : Phone(identity) {
override fun usePhone() {
println("${identity.getIdentity()}功能类型手机")
}
}
二、主程序调用
package com.study.bridge
fun main() {
val studentIdentity = StudentIdentity()
val bossIdentity = BossIdentity()
val studentCameraPhone = CameraPhone(studentIdentity)
studentCameraPhone.usePhone()
val bossCameraPhone = CameraPhone(bossIdentity)
bossCameraPhone.usePhone()
val studentFunctionPhone = FunctionPhone(studentIdentity)
studentFunctionPhone.usePhone()
val bossFunctionPhone = FunctionPhone(bossIdentity)
bossFunctionPhone.usePhone()
}
三、运行结果