Kotlin-尝试使用高阶函数分解代码
我是Kotlin的新手,我想看看使用高阶函数是否对我的情况有所帮助 我的用例是我需要调用一个IInterface派生类的方法来向一个或多个组件发送事件。我想让这个通用化,我想看看高阶函数是否有帮助。一个代码示例将有助于理解(我希望如此!)Kotlin-尝试使用高阶函数分解代码,kotlin,Kotlin,我是Kotlin的新手,我想看看使用高阶函数是否对我的情况有所帮助 我的用例是我需要调用一个IInterface派生类的方法来向一个或多个组件发送事件。我想让这个通用化,我想看看高阶函数是否有帮助。一个代码示例将有助于理解(我希望如此!) private val eventListeners=mutableListOf()//列表已在其他地方填充! private fun sendConnectionEvent(dummyString:String){ val deadListeners=mut
private val eventListeners=mutableListOf()//列表已在其他地方填充!
private fun sendConnectionEvent(dummyString:String){
val deadListeners=mutableListOf()
eventListeners.forEach{
试一试{
it.onConnectionEvent(dummyString)
}catch(e:DeadObjectException){
d(标记“删除侦听器-异常${e.message}”)
添加(它)
}
}
deadListeners.forEach{it->
eventListeners.remove(它)
}
}
私人娱乐sendWonderfulEvent(dummyString:String,dummyInt:Int){
val deadListeners=mutableListOf()
eventListeners.forEach{
试一试{
it.onWonderfulEvent(dummyString,dummyInt)
}catch(e:DeadObjectException){
d(标记“删除侦听器-异常${e.message}”)
添加(它)
}
}
deadListeners.forEach{it->
eventListeners.remove(它)
}
}
我添加了两种类似的方法(在实际用例中我会有更多),我认为(我希望!)可以做一些事情,但我不能让高阶函数在这种情况下工作,因为:
- 我想在几个实例上调用相同的方法,而不是“仅仅”一个基本函数
- 更糟糕的是,我需要调用的方法没有相同的原型(那太容易了!)李>
VR以下是如何做到这一点
fun onEvent(body: (IEventInterface) -> Unit) {
val deadListeners = mutableListOf<IEventInterface>()
eventListeners.forEach {
try {
body(it)
} catch (ex: DeadObjectException) {
Log.d(TAG, "Removing listener - Exception ${e.message}")
deadListeners.add(it)
}
}
deadListeners.forEach { it ->
eventListeners.remove(it)
}
}
fun-onEvent(主体:(ieventerface)->单元){
val deadListeners=mutableListOf()
eventListeners.forEach{
试一试{
正文(it)
}捕获(例如:DeadObjectException){
d(标记“删除侦听器-异常${e.message}”)
添加(它)
}
}
deadListeners.forEach{it->
eventListeners.remove(它)
}
}
- 假设这样的接口:
interface IEventInterface { fun onConnectionEvent(dummyString: String) fun onWonderfulEvent(dummyString: String, dummyInt: Int) }
- 定义实现已定义接口的泛型类型(
) - 定义此类型的可变列表以接收实现(
)MutableList.removiefthrows
- 期望为您的类型提供一个扩展函数,用于执行特定的验证(如果需要,还可以自定义参数)
- 使用apply并返回实例,您可以像管道一样运行代码
- 在需要时执行自定义验证
private fun <T : IEventInterface> MutableList<T>.removeIfThrows(validation: T.() -> Unit, customLogMessage: String? = null): MutableList<T> { return apply { removeIf { it.runCatching { validation() }.onFailure { error -> print(customLogMessage ?: "Removing listener - Exception ${error.message}") }.isFailure } } }
private fun <T : IEventInterface> MutableList<T>.removeIfThrows(validation: T.() -> Unit, customLogMessage: String? = null): MutableList<T> {
return apply {
removeIf {
it.runCatching {
validation()
}.onFailure { error ->
print(customLogMessage ?: "Removing listener - Exception ${error.message}")
}.isFailure
}
}
}
private fun <T : IEventInterface> MutableList<T>.sendConnectionEvent(dummyString: String) = removeIfThrows({
onConnectionEvent(dummyString)
})
private fun <T : IEventInterface> MutableList<T>.sendWonderfulEvent(dummyString: String, dummyInt: Int) = removeIfThrows({
onWonderfulEvent(dummyString, dummyInt)
})
private fun nowYouCanDoSomethingLikeThis() {
eventListeners
.sendConnectionEvent("some dummy string")
.sendWonderfulEvent("some another dummy string", 123)
}