Kotlin Delegation 如何从委托对象访问 class 属性
Kotlin Delegation how to access class properties from delegate object
我正在尝试从巨人 class 中拆分一些工作以提供更多可读性。首先我查看了 Extension,但它似乎只是在创建一些静态函数,然后委托模式进入了我的眼帘。
下面的代码看起来没问题,委托就像 EnhancedProducer class 的一部分一样工作。
但是有一个问题阻碍了我,我不太明白如何从委托访问 EnhancedProcuder class 的服务 属性。在我的真实代码中,有些情况下原始class和委托class都需要同时使用服务变量,所以不知道有没有办法做到.
我知道我们可以将服务实例注入到它们中,但我仍然想知道是否有更优雅的方式让委托更自然地适应 EnhancedProducer class。
interface Producer {
fun produce()
}
class ProducerImpl : Producer {
override fun produce() {
// service.doSomething() how to access service here
println( "ProducerImpl")
}
}
class EnhancedProducer(private val delegate: Producer) : Producer by delegate {
// how to share this with delegate
//private val service = Service()
fun test() {
produce()
}
}
fun main() {
val producer = EnhancedProducer(ProducerImpl())
producer.test()
}
可以在界面中使用开放属性:
interface Producer {
fun produce()
// two classes will use/modify this property
var service: Service
}
...
class ProducerImpl: Producer {
override var service = Service()
fun changeService() {
service.execute() // access to the interface field
}
}
...
class EnhancedProducer(private val delegate: Producer): Producer by delegate {
fun test() {
this.service // access to the interface field
delegate.service // access to the interface field
produce()
}
}
fun main() {
val producerImpl = ProducerImpl()
val producer = EnhancedProducer(producerImpl)
producerImpl.service // access to the interface field
producer.service // access to the interface field
}
我最终想出了一个解决方案,可以在关键字之后立即初始化 ProducerImpl。奇怪的是,到目前为止我找到的所有示例都只尝试注入一个实例,而不是在需要委托时提供初始化。也许有人对此有所了解?
interface Producer {
fun produce()
}
class ProducerImpl(val service:Service) : Producer {
override fun produce() {
service.doSomething()
println(item)
}
}
class EnhancedProducer(val service:Service) : Producer by ProducerImpl(service) {
fun test() {
produce()
}
}
fun main() {
val service = Service()
val producer = EnhancedProducer(service)
}
我正在尝试从巨人 class 中拆分一些工作以提供更多可读性。首先我查看了 Extension,但它似乎只是在创建一些静态函数,然后委托模式进入了我的眼帘。
下面的代码看起来没问题,委托就像 EnhancedProducer class 的一部分一样工作。 但是有一个问题阻碍了我,我不太明白如何从委托访问 EnhancedProcuder class 的服务 属性。在我的真实代码中,有些情况下原始class和委托class都需要同时使用服务变量,所以不知道有没有办法做到.
我知道我们可以将服务实例注入到它们中,但我仍然想知道是否有更优雅的方式让委托更自然地适应 EnhancedProducer class。
interface Producer {
fun produce()
}
class ProducerImpl : Producer {
override fun produce() {
// service.doSomething() how to access service here
println( "ProducerImpl")
}
}
class EnhancedProducer(private val delegate: Producer) : Producer by delegate {
// how to share this with delegate
//private val service = Service()
fun test() {
produce()
}
}
fun main() {
val producer = EnhancedProducer(ProducerImpl())
producer.test()
}
可以在界面中使用开放属性:
interface Producer {
fun produce()
// two classes will use/modify this property
var service: Service
}
...
class ProducerImpl: Producer {
override var service = Service()
fun changeService() {
service.execute() // access to the interface field
}
}
...
class EnhancedProducer(private val delegate: Producer): Producer by delegate {
fun test() {
this.service // access to the interface field
delegate.service // access to the interface field
produce()
}
}
fun main() {
val producerImpl = ProducerImpl()
val producer = EnhancedProducer(producerImpl)
producerImpl.service // access to the interface field
producer.service // access to the interface field
}
我最终想出了一个解决方案,可以在关键字之后立即初始化 ProducerImpl。奇怪的是,到目前为止我找到的所有示例都只尝试注入一个实例,而不是在需要委托时提供初始化。也许有人对此有所了解?
interface Producer {
fun produce()
}
class ProducerImpl(val service:Service) : Producer {
override fun produce() {
service.doSomething()
println(item)
}
}
class EnhancedProducer(val service:Service) : Producer by ProducerImpl(service) {
fun test() {
produce()
}
}
fun main() {
val service = Service()
val producer = EnhancedProducer(service)
}