我通常使用特徵實施策略(一些行動,不需要附帶的領域)。最近我發現可以用對象來定義相同的功能。它們可直接擴展功能性狀或應用()什麼是最好的策略在斯卡拉:對象或特質
示例代碼擴展預留一些特質定義特殊的方法:
/* strategy realization via traits */
package object Traitness {
trait Strategy {
def performAction() : Unit =()
}
abstract class Usage {_ : Strategy =>
def doWork() =
this.performAction()
}
// defining strategies
trait SayA extends Strategy {
override def performAction() = {
println("A")
super.performAction()
}
}
trait SayB extends Strategy {
override def performAction() = {
println("B")
super.performAction()
}
}
trait SayC extends Strategy {
override def performAction() = {
println("C")
super.performAction()
}
}
//using strategies
class SimpleStrategy extends Usage with SayA
def reverseOrder() = new Usage with SayC with SayA
object fullUsage extends Usage with SayA with SayB with SayC
//run-time checking
val check1 : Boolean = (new SimpleStrategy).isInstanceOf[SayB]
val check2 : Boolean = reverseOrder().isInstanceOf[SayB]
val check3 : Boolean = fullUsage.isInstanceOf[SayB]
//compile-time checking
def proclaim(x : SayB) = println("SayB")
}
/* strategy realization via function objects */
package object Valueness {
trait Strategy extends Function0[Unit]
class Usage(val strategies : List[Strategy]) {
def doWork() = for (s <- strategies)
s()
}
//defining strategies
object SayA extends Strategy {
override def apply() = {
println("A")
}
}
object SayB extends Strategy {
override def apply() = {
println("B")
}
}
object SayC extends Strategy {
override def apply() = {
println("C")
}
}
//using strategies
class SimpleStrategy extends Usage(SayA :: Nil)
def reverseOrder() = new Usage(SayB :: SayA :: Nil)
val fullUsage = new Usage(SayA :: SayB :: SayC :: Nil)
//run-time checking
def check(strategy : Strategy, usage : Usage) = usage.strategies contains strategy
val check1 : Boolean = check(SayB, new SimpleStrategy)
val check2 : Boolean = check(SayB, reverseOrder())
val check3 : Boolean = check(SayB, fullUsage)
//no compile-time checking available
}
哪一個我應該選擇?
這是非常相似的成分(proxy'ing來處理不同的策略對象)與繼承(其中實現不同性狀混合戰略)..我認爲這不是天生「最好」(或「更可取」),不同的情況可能適合不同的策略。 – 2012-12-01 18:37:55
使用自我類型不是強制性的。我可以混合使用策略,並將它們作爲類型策略的類成員包含在用法中。 – ayvango