我想创建一个框架,用户可以在其中对基类进行子类化,Node
该基类能够根据当前实例状态和另一个实例的属性(参数 n)生成消息(此处为整数)。用户应该能够根据当前实例的类和参数 n 的类getMessage
来专门化该方法以生成不同的消息,如下面的代码所示。
该函数importantAlgorithm
使用这些节点生成的消息来计算最终结果。
// Classes defined by the framework //
abstract class Node {
def getMessage(n: Node) : Int
}
def importantAlgorithm(lstNodes1: List[_ <: Node], lstNodes2: List[_ <: Node]) = {
val results = lstNodes1.zip(lstNodes2).map({case (n1, n2) =>
// I would like to get the proper message *BASED ON
// THE TYPE OF N1 and N2*
val message = n1.getMessage(n2)
// Do some work with the message
//...
//...
})
//...
}
// Classes defined by framework users //
class ItemNode(val p: Int) extends Node {
override def getMessage(n: UserNode) = {
// Compute message based on this ItemNode member variables
// and n (instance of UserNode) member variables
}
override def getMessage(n: ItemNode) = {
// Compute message based on this ItemNode member variables
// and n (instance of UserNode) member variables
// The algorithm is different from the algorithm
// used in the previous method
}
}
class UserNode extends Node {
override def getMessage(n: OtherNode) = {
// Compute message. Same idea as above
}
}
class OtherNode extends Node {
override def getMessage(n: UserNode) = {
// Compute message. Same idea as above
}
}
// The user should be able to use the framework this way
importantAlgorithm(List(new UserNode(), new ItemNode(236), new OtherNode(),
List(new OtherNode(), new ItemNode(542), new UserNode()))
当然,Scala 不允许在子类中特化方法的参数,上述代码无法编译。我可以使用isInstanceOf[]
或 RTTI,但我觉得我没有正确思考并且没有正确设计我的框架。如何用更简单、更清洁的解决方案替换上面代码示例中描述的机制?