创建实现 2 个接口的单例对象
Create singleton object which implements 2 interfaces
single<ObserveLifeCycleState> { Communicator( domain = get(), repositoryMessanger = get() ) }
single<NewMessageListener> { Communicator( domain = get(), repositoryMessanger = get() ) }
如您所见,我正在实例化相同的 2 个对象,因为它实现了两个接口。但这会创建 2 个不同的对象。只用一次初始化怎么办?
single {
Communicator(
domain = get(),
repositoryMessanger = get()
)
}
single {
val communicator: Communicator = get()
communicator as NewMessageListener
}
这是将单个实例绑定到多个接口的惯用方法。
single {
Communicator(get(), get())
}
single<NewMessageListener> {
get<Communicator>()
}
single<ObserveLifeCycleState> {
get<Communicator>()
}
single<ObserveLifeCycleState> { Communicator( domain = get(), repositoryMessanger = get() ) }
single<NewMessageListener> { Communicator( domain = get(), repositoryMessanger = get() ) }
如您所见,我正在实例化相同的 2 个对象,因为它实现了两个接口。但这会创建 2 个不同的对象。只用一次初始化怎么办?
single {
Communicator(
domain = get(),
repositoryMessanger = get()
)
}
single {
val communicator: Communicator = get()
communicator as NewMessageListener
}
这是将单个实例绑定到多个接口的惯用方法。
single {
Communicator(get(), get())
}
single<NewMessageListener> {
get<Communicator>()
}
single<ObserveLifeCycleState> {
get<Communicator>()
}