我目前正在使用 Cake Pattern 来实现一些优化算法。我经常遇到名称冲突问题。例如:
trait Add[T] { this: Foo[T] =>
def constant: T
def plus( t1: T, t2: T ): T
def add( t: T ) = plus( t, constant )
}
trait Mul[T] { this: Bar[T] =>
def constant: T
def times( t1: T, t2: T ): T
def mul( t: T ) = times( t, constant )
}
trait Operations[T] { this: Add[T] with Mul[T] =>
def neg( t: T ): T
}
在这里,
constant
在 Add
中都定义了和 Mul
特质,但它们的值(value)可能不同。我可以用特征名称作为名称的前缀,但我发现它丑陋而脆弱( def mulConstant: T
)。有没有更好的方法呢?
最佳答案
据我所知,传统的蛋糕模式通常涉及 1 层特征嵌套,将操作组合在一起。然后,外层声明了实际的“服务”(这里是:Add、Mul、Operations)而不定义它。
trait AddComponent[T] { this: FooComponent[T] =>
def addition: Add
trait Add {
def constant: T
def plus( t1: T, t2: T ): T
def add( t: T ) = plus( t, constant )
}
}
trait MulComponent[T] { this: BarComponent[T] =>
def multiplication: Mul
trait Mul {
def constant: T
def times( t1: T, t2: T ): T
def mul( t: T ) = times( t, constant )
}
}
trait OperationsComponent[T] { this: Add[T] with Mul[T] =>
def operations: Operations
trait Operations {
def neg( t: T ): T
}
}
然后,当将“...Component”特征混合在一起时,依赖关系被连接起来:
trait IntOperations extends Operation[Int] {
class IntAdd extends Add { ... }
class IntMul extends Mul { ... }
}
class MyFooBar extends FooComponent[Int] with BarComponent[Int] with IntOperations {
lazy val addition = new IntAdd
lazy val multiplication = new IntMul
lazy val foo = ...
lazy val bar = ...
}
这解决了您特定的命名空间问题,但名称冲突(“服务”定义)仍然是传统蛋糕模式的问题。有一个blog post作者:Daniel Spiewak 演示了一般如何解决这个问题,但该解决方案有其自己的一组(巨大的)权衡(参见 this talk)。
希望有所帮助。
附言在这里使用抽象类型而不是类型参数可能会更好
关于scala - 避免名称与 Cake Pattern 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16519664/