使用责任链模式,我遇到了一个问题,期望下一个链式元素具有相同的第一个元素的泛型类型。我知道为什么会发生这种情况:第一个处理程序期望第二个处理程序使用泛型类型“”。我只是不知道该怎么解决。
关于如何使用handle it in java here,有一个答案,但是由于java没有具体化类型,所以在Kotlin中,这种方法看起来应该是不同的,对吗?
我想到了不同的选择:
为了说明这个问题,我在下面发布了一个演示代码。
data class Apple(val name:String, val color:Int)
data class Orange(val circumference:Double)
object Main{
@JvmStatic
fun main(args: Array<String>) {
val first = FirstHandler()
val second = SecondHandler()
first.setNextHandler(second) // !!! wrong type here since <Apple> is expected
first.process()
}
}
abstract class ChainHandler<T>{
protected var nextHandlerInChain:ChainHandler<T>? = null
fun setNextHandler(handler: ChainHandler<T>): ChainHandler<T> {
this.nextHandlerInChain = handler
return handler
}
abstract fun peel(): Collection<T>
abstract fun process():MutableMap<String,Any> }
class FirstHandler : ChainHandler<Apple>() {
override fun peel(): Collection<Apple> {
return Collections.emptyList<Apple>()
}
override fun process(): MutableMap<String, Any> {
val peeledApples = peel()
val map = nextHandlerInChain?.process()
map?.put("apples",peeledApples) ?:kotlin.run {
val map = mutableMapOf<String,Any>()
map.put("apples",peeledApples)
}
return map!!
} }
class SecondHandler : ChainHandler<Orange>() {
override fun peel(): Collection<Orange> {
return Collections.emptyList<Orange>()
}
override fun process(): MutableMap<String, Any> {
val peeledOranges = peel()
val map = nextHandlerInChain?.process()
map?.put("oranges",peeledOranges) ?:kotlin.run {
val map = mutableMapOf<String,Any>()
map.put("oranges",peeledOranges)
}
return map!!
}
}发布于 2018-09-24 21:16:54
Kotlin有一种叫做星体投影的东西也许能帮到你。它基本上告诉编译器,您并不真正关心您得到的ChainHandler类型。您可以使用它来编译setNextHandler,如下所示:
abstract class ChainHandler<T>{
// Note the star projection here
protected var nextHandlerInChain: ChainHandler<*>? = null
// Defining a type parameter S, so that the return type is equal to the input type.
fun <S> setNextHandler(handler: ChainHandler<S>): ChainHandler<S> {
this.nextHandlerInChain = handler
return handler
}
...
}你可以在这里读到更多关于恒星投影的信息:https://kotlinlang.org/docs/reference/generics.html#star-projections
至于类型参数的具体化:具体化的类型参数只适用于内联函数。不适用于类上的类型参数。
发布于 2018-09-24 21:17:02
这在很大程度上取决于希望处理程序如何交互的细节。对于这段代码,只需将下一个处理程序的类型(在var nextHandlerInChain和fun setNextHandler中都更改为ChainHandler<*> )就可以了,因为process()无论如何都会返回与T无关的内容。
https://stackoverflow.com/questions/52487069
复制相似问题