我对 VIPER 架构中协议的好处有点困惑。我了解 DI(依赖注入)通过协议实现,有助于避免对象之间的直接依赖 - 我同意。
但我正在从使用的角度来看真正的好处,一个例子可能是 - 特别是协议如何帮助单元测试(测试交互器部分)受益。
我们不能通过方法回调的 using 块来实现相同的目的吗?希望有人可以通过一些例子帮助我从使用的角度理解
干杯
我是Swift编程的新手,我在构建Factory设计模式方面遇到了阻碍.以下是我的代码:
protocol IInterface {
func InterfaceMethod() -> Void
}
public class SubClass: IInterface {
init() {}
func InterfaceMethod() { }
public func SubClassMethod() { }
}
public class Factory() {
class func createFactory() -> IInterface {
return SubClass()
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我试图像下面这样访问它
let mgr = Factory.createFactory()
//calling Interface Method
mgr.InterfaceMethod() -- This works, when i keep a dot (mgr.) it shows the method name
//calling subclass method
mgr.SubClassMethod() -- This doesn't work, when i keep a dot (mgr.) it doesnt even …Run Code Online (Sandbox Code Playgroud)