4

我通常使用特征来实施策略(一些不需要附带字段的操作)。最近我发现可以根据对象定义相同的功能。它们可能是直接扩展 Function trait 或扩展了一些除 apply() 之外定义特殊方法的 trait

示例代码:

/* 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
}

我应该选择哪一个?

4

2 回答 2

4

在您描述的用例中,使用特征或对象是不必要的面向对象的过度杀伤。您的策略只是简单的功能,并且可以最干净地实现。

object Strategy{

  type Strategy = () => Unit

  val sayA:Strategy = ()=>{println("A")}
  val sayB:Strategy = ()=>{println("B")}
  val sayC:Strategy = ()=>{println("C")}
}

在这里创建 Function0[Unit] 的子类不会给您带来任何好处,并且会让您失去制作微不足道的文字的能力。函数在 Scala 中是非常好的实体。熟悉它们,不要犹豫直接使用它们。

于 2012-12-01T19:25:52.763 回答
0
class MyStrategy {
  val sayABC: Strategy.Strategy = () => {
    Strategy.sayA()
    Strategy.sayB()
    Strategy.sayC()
  }
}
reflect.runtime.universe.typeOf[MyStrategy].members.filter(_.asTerm.fullName.endsWith(".sayABC")).head

结果:

res36: reflect.runtime.universe.Symbol = value sayABC

如果我的函数 sayABC 仅包含对不同函数的调用,那么如果有人向我们展示如何通过 AST 获取对 sayA、sayB、sayC 的调用,那就太好了?

...它旨在成为对 Dave Griffith 代码的格式正确的答案

于 2012-12-01T20:52:26.560 回答