假设我的应用程序中有两个依赖项,一个连接到某个 pub 子系统,一个连接到数据库。我可以做类似的事情
trait DB {
def lookup(query:String):String
}
trait PubSub {
def subscribe(key:String, callback:String => Any)
}
然后我可以像这样写我的逻辑
trait Functionality { this:DB with PubSub =>
def doSomething() {
val key = lookup("get key")
subscribe(key, data => println(data))
}
}
然后我的应用程序可以像
object Awesome extends App {
object repository extends Functionality with DB with PubSub {
def lookup(query:String) = "some key"
def subscribe(key:String, callback:String => Any) {
scala.concurrent.ops.spawn { while(true) { callback(key) ; Thread.Sleep(1000) } }
}
}
repository.doSomething()
}
世界上一切都很好。
但是,如果我想要连接到在同一个应用程序中共享相同数据库实现的两个 pub 子系统怎么办?
我想做类似的事情
object Awesome2 extends App {
object repository extends DB {
def lookup(query: String): String = "some other key"
object connection1 extends Functionality with PubSub with DB {
def subscribe(key: String, callback: (String) => Any) {
scala.concurrent.ops.spawn { while(true) { callback(key.toUpperCase) ; Thread.sleep(1000) } }
}
}
object connection2 extends Functionality with PubSub with DB {
def subscribe(key: String, callback: (String) => Any) {
scala.concurrent.ops.spawn { while(true) { callback(key.toLowerCase) ; Thread.sleep(1000) } }
}
}
}
}
第二层蛋糕中的对象(隐含地?)在父级别的数据库实现中啜饮。
但是scala编译器告诉我
error: object creation impossible, since method lookup in trait DB of type (query:String) String is not defined
object connection2 extends Functionality with PubSub with DB {
如果我执行以下操作,那么它会执行我想要的操作
object Awesome3 extends App {
object repository extends DB {
override def lookup(query: String): String = "some other key"
object connection1 extends Functionality with PubSub with DB {
def subscribe(key: String, callback: (String) => Any) {
scala.concurrent.ops.spawn { while(true) { callback(key.toUpperCase) ; Thread.sleep(1000) } }
}
def lookup(query: String): String = repository.lookup(query)
}
object connection2 extends Functionality with PubSub with DB {
def subscribe(key: String, callback: (String) => Any) {
scala.concurrent.ops.spawn { while(true) { callback(key.toLowerCase) ; Thread.sleep(1000) } }
}
def lookup(query: String): String = repository.lookup(query)
}
}
repository.connection1.doSomething()
repository.connection2.doSomething()
}
但这有点乱
我可以添加这个特征
trait DB_Base extends DB {
private val db:DB = this
trait DB_Layer extends DB {
def lookup(query:String):String = db.lookup(query)
}
}
然后下面的作品
object Awesome4 extends App {
object repository extends DB_Base {
override def lookup(query: String): String = "some other key"
object connection1 extends Functionality with PubSub with DB_Layer {
def subscribe(key: String, callback: (String) => Any) {
scala.concurrent.ops.spawn { while(true) { callback(key.toUpperCase) ; Thread.sleep(1000) } }
}
}
object connection2 extends Functionality with PubSub with DB_Layer {
def subscribe(key: String, callback: (String) => Any) {
scala.concurrent.ops.spawn { while(true) { callback(key.toLowerCase) ; Thread.sleep(1000) } }
}
}
}
repository.connection1.doSomething()
repository.connection2.doSomething()
}
所以现在我有两层。如何获得三个?我觉得我正在失去剧情。