gpt4 book ai didi

Swift:如何支持 "template method"设计模式(因为 Swift 没有保护)?

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:49 25 4
gpt4 key购买 nike

传统上,在“模板方法”模式中,基类实现一些算法,并为特定行为遵从派生类。这在 C++/C#/Java 等语言中效果很好,因为您可以在这些方法上使用“protected”来对调用者隐藏它们,但对派生类保持可见。例如,在 GoF 书中,你有这样的东西:

class Application
{
void CreateDocument() { ..., this->DoCreateDocument() }
protected void DoCreateDocument() { } // override for custom behavior
}

这使 Application 的公共(public)接口(interface)保持干净。在 Swift 中,因为不能使用 protected,公共(public)接口(interface)不干净。我不希望 Application 的用户看到 DoCreateDocument

所以我正在尝试另一种方法,而不是使用 DoCreateDocument 的方法,我试图定义一个闭包并使用“仿函数”模式。

class Application
{
typealias ActionFunc = () -> ()
private let doCreateDocument : ActionFunc
init(a : ActionFunc) { self.doCreateDocument = a }
func CreateDocument() {
self.doCreateDocument()
}
}

所以这个类看起来不错——公共(public)接口(interface)很干净。但是,实际使用它是不可能的。

显而易见的方法是使用派生类:

class DoApplication : Application
{
init() {
super.init(
a : {
// This would work, but you cannot use self here!
self. // anything with self. is an error
})
}
}

这种方法的问题在于,在初始化程序中,您不能将闭包传递给使用 selfsuper.init。我收到错误 self used before super.init

这基本上使它变得毫无用处,因为您无法访问任何状态变量。

但是,如果您不在 init 中初始化 doCreateDocument,则需要公开某种 setter - 同样,缺少 protected 表示 setter 在公共(public) API 上。呸。

那么有什么方法可以干净地实现保持界面清洁的模板模式吗?

最佳答案

我知道这是一个丑陋的 hack,但它有效。

class DoApplication: Application {
init() {
var doCreateDocument: (Application.ActionFunc)!
super.init(a: { doCreateDocument() })
doCreateDocument = { [unowned self] in
self.foo()
}
}

func foo() {
println("DoApplication.foo");
}
}

或者,您可以将 self 传递给 doCreateDocument:

class Application {
typealias ActionFunc = (Application) -> ()
private let doCreateDocument : ActionFunc

init(a : ActionFunc) { self.doCreateDocument = a }

func CreateDocument() {
self.doCreateDocument(self)
}
}

class DoApplication : Application {
init() {
super.init(a : { _self in
let _self = _self as! DoApplication

_self.foo()
})
}
func foo() {
println("DoApplication.foo");
}
}

关于Swift:如何支持 "template method"设计模式(因为 Swift 没有保护)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31757514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com