【发布时间】:2023-06-23 04:52:02
【问题描述】:
受我对通用策略模式的 C# 实现的启发,我想在 Scala 中做同样的事情。我还想做一些函数式编程来将策略算法封装在继承的类中。 所以我现在做的是:
trait Strategy {
type T <: Strategy
type O
def Call(victim: T): O = {
strategy(victim)
}
var strategy: (this.T => this.O)
}
这是一个特征,它是烫伤的基础。我还有一个StrategyFactory 类:
case class StrategyFactory[T <: Strategy, O](str: T) {
def Call(x: (T => O)) = x(str)
}
object StrategyFactory {
}
最后在我的代码中我可以创建具体的策略:
class DownloadStrategy(path: String) extends Strategy {
type T = DownloadStrategy
type O = String
strategy = (dw: DownloadStrategy) => path + "aaaa"
}
object DownloadStrategy {
def apply(s: String) = new DownloadStrategy(s)
}
在我的应用程序代码中,我有这个:
var ds = DownloadStrategy("j")
val m = StrategyFactory[DownloadStrategy, String](ds)
var output = m.Call(ds.strategy)
这里一切正常。
我想要功能性策略,因此有m.Call(ds.strategy)
但这是非常愚蠢的设计,因为我无法创建一组将扩展 DownloadStrategy 的类。例如:
class ImageDownloadStrategy(w: String, h: String, path: String) extends DownloadStrategy(path){
type T = ImageDownloadStrategy
type O = String
strategy = (ids: T) => path + ":ImageDownloadStrategy"
}
class VideoDownloadStrategy(w: String, h: String, path: String) extends DownloadStrategy(path){
type T = VideoDownloadStrategy
type O = String
strategy = (ids: T) => path + ":VideoDownloadStrategy"
}
等等。基本上我希望有一些默认策略的基类,子类是更具体的实现。
这让我想到了我想编写如下代码的应用程序代码:
var ds: DownloadStrategy = null
request.getQueryString("t") match {
case "1" => ds = ImageDownloadStrategy("","","")
case "2" => ds = VideoDownloadStrategy("","","")
case "3" => ds = RawFileDownloadStrategy("","","")
case _ => ds = DownloadStrategy("")
}
var output = (StrategyFactory[DownloadStrategy, String](ds)).Call(ds.strategy)
我认为当我编写StrategyFactory[DownloadStrategy, String](ds) 时,编译器会非常聪明,可以判断ImageDownloadStrategy 是否是DownloadStrategy 的子类可以让我进行一些多态调用,但我做不到。
另一个事实是,我需要在 DownloadStrategy 的交付类中覆盖 type T 和 type O,但我不知道该怎么做。
请给我一些建议如何模拟这种行为。
编辑(关于 pagoda_5b 的详细信息)
正如我所提到的,我在trait Strategy 中具有功能性var strategy,即var strategy: (this.T => this.O)。这个变量需要在实现这个特性的类中被覆盖。我还有 2 个泛型类型,T 表示具体策略的子类,O 表示来自def Call(...) 的结果类型。
我想要实现的是在 Strategy 的子类中拥有功能性策略,然后进行多态调用。在这里,我有DownloadStrategy 这是默认策略,并且我有一些带有特定算法的子类。我想将 ImageDownloadStrategy 转换为 DownloadStrategy 并按照我在 switch case 语句中显示的那样使用它。
【问题讨论】:
-
所有这些工作都可以通过使用函数作为值来轻松替换,这是函数式编程的一大好处,也是 scala 的一大好处。如果您不提供有关单个策略应如何使用其构造参数(即 w、h、路径)的更多详细信息,我将无法写出完整的答案
-
strategy成员可变这一事实重要吗?如果是这样,从功能的角度来看,我不喜欢这种模式。如果不是,那只是一个函数T => O,不是吗? -
@ziggystar 我不知道 mutable 是什么意思?意思是可变的?我同意 strategy 可以是 val,但需要在子类中覆盖或实施。
-
这里的可变意味着使用
var而不是val,这使得Strategytrait 可变。您的子类仍然可以覆盖val。两者的区别在于不可变的值不能被重新赋值,有点像final或constant。
标签: scala design-patterns strategy-pattern