- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试快速使用工厂模式,给出我的代码
我有两个协议(protocol)
protocol MyProtocol1{
func callLoginAPI()
}
protocol MyProtocol2{
func callPaymentAPI()
}
我有两个符合这些协议(protocol)的结构
struct MyManager1: MyProtocol1{
func callLoginAPI()
{
debugPrint("Inisde--MyManager1 and ready to call an Login API")
}
}
struct MyManager2: MyProtocol2{
func callPaymentAPI()
{
debugPrint("Inisde--MyManager2 and ready to call Payment API")
}
}
我想使用工厂模式通过传递协议(protocol)并返回符合该协议(protocol)的结构的具体对象来创建管理器的实例
示例: ManagerFactory.create(MyProtocol1) --> 应该给我 MyManager1 的实例并且执行 ManagerFactory.create(MyProtocol2) --> 应该给我 MyManager2 的实例
我认为这可能行不通,因为我在运行时要求具体类型,所以我最终做了这样的事情
protocol IManagerFactory{
func getMyManager1() -> MyProtocol1
func getMyManager2() -> MyProtocol2
}
struct ManagerFactory: IManagerFactory
{
func getMyManager1() -> MyProtocol1 {
return MyManager1()
}
func getMyManager2() -> MyProtocol2 {
return MyManager2()
}
}
但我很好奇我在示例中尝试做的事情是否可以实现,我使用的是 swift 4.2。
我见过其他示例,但它们都具有相同的协议(protocol),它们遵循相同的协议(protocol),例如矩形、正方形和圆形遵循相同的协议(protocol)形状。
在我的例子中,我有两个独立的协议(protocol),它们做完全不同的事情,所以我在这个例子中尝试做的事情是否有可能?或者我最终采用的方式是唯一的解决方法。
请建议最好的方法是什么。
最佳答案
首先,我非常怀疑“管理器”是一个值(一个结构)而不是一个实例(一个类)。你真的打算在这里使用结构和协议(protocol)吗?结构没有标识;具有相同属性的两个结构必须可以完全互换,而类似的东西通常不会被命名为“manager”。
你所描述的当然是可写的。这有点没用。写法如下:
struct ManagerFactory {
static func create(_ type: MyProtocol1.Protocol) -> MyProtocol1 {
return MyManager1()
}
static func create(_ type: MyProtocol2.Protocol) -> MyProtocol2 {
return MyManager2()
}
}
let mgr1 = ManagerFactory.create(MyProtocol1.self)
let mgr2 = ManagerFactory.create(MyProtocol2.self)
但这只是使用方法重载替换名称的一种精心设计的方法。
你似乎想要的是一个单一的方法,但它的返回类型是什么?即使在 C# 中,如果不添加令人讨厌的向下转换,我也不认为这是可写的。 (这是您和 paulw11 在评论中讨论的重点。)这不是 Swift 的限制;这是类型的基本特征。即使在 JavaScript 中,您也需要知道您期望返回什么,否则您将不知道可以调用哪些方法(只是您在头脑和文档中而不是在编译器和代码中跟踪这种期望) .
您似乎在这里创建了很多协议(protocol),我敢打赌它们大多只有一个实现。那是糟糕的 swift 。除非您有特定需求,否则不要创建协议(protocol)。不要为了好玩而创造景点。他们会很快烧死你。
如果您的协议(protocol)确实看起来像这样,并且您真的对这些方法有不同的实现(即,如果只有一个 MyProtocol1
实现,请不要这样做”以防万一”),你真正想要的是函数:
struct API {
let login: () -> Void
let payment: () -> Void
}
let myAPI = API(login: myLoginFunction, payment: myPaymentFunction)
这将允许您在需要时创建不同的 API。但同样,只有当您需要这种灵 active 时。如果没有,只需对实例使用类,对值使用结构,并避免使用协议(protocol),直到您的程序中至少有 2 个,最好是 3 个实现。
关于ios - 快速使用不同协议(protocol)的工厂模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54497200/
我可以看到有状态的协议(protocol)可以减少像 cookie 这样的“模拟状态”。 但是测试变得更加难以确保您的实现正确并重新连接,并且 session 继续可能很难处理。 始终使用无状态协议(
我正在尝试为我的下一个分布式应用程序找到合适的协议(protocol)中间件。在过去的几天里,我找到了几个规范,想知道我是否错过了一个重要的规范?它应该是二进制协议(protocol),支持 RPC,
我正在做一个研究生院软件工程项目,我正在寻找管理 ATM 和银行网络之间通信的协议(protocol)。 我已经在谷歌上搜索了很长一段时间,虽然我找到了各种有关 ATM 的有趣信息,但我惊讶地发现似乎
我正在开发一个 ECG 模块,它以字节为单位给出数据。有一个关于它的协议(protocol)文档解释了如何构建从模块中出来的数据包。我想解码该数据。我很困惑 Protocol Buffer 是否会对此
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 3年前关闭。 Improve this qu
我需要在我的程序中包含基本的文件发送和文件接收例程,并且需要通过 ZMODEM 协议(protocol)。问题是我无法理解规范。 供引用,here is the specification . 规范没
我最近听到这个术语来描述 Google 的新环聊协议(protocol)和 Whisper System 的新 encrypted texting app . The new TextSecure p
如何检查某个对象是否符合协议(protocol)? 我试过这种方式,但出现错误: if lCell.conformsToProtocol(ContentProtocol) { } 最佳
在应用程序中,我们有两种类型的贴纸,字符串和位图。每个贴纸包都可以包含两种类型。这就是我声明模型的方式: // Mark: - Models protocol Sticker: Codable { }
这个问题在这里已经有了答案: Why can't a get-only property requirement in a protocol be satisfied by a property w
我有以下快速代码: protocol Animal { var name: String { get } } struct Bird: Animal { var name: String
我在遵循继承树的几个类中分配协议(protocol)。像这样: 头等舱 @protocol LevelOne - (void) functionA @end @interface BaseClass
我们之前使用的是 fix,但客户说使用 OUCH 进行交易,因为这样速度更快。我在互联网上查了一下,消息看起来很相似。它如何获得速度优势。请给我一些示例消息 最佳答案 基本上,FIX 消息以文本格式传
在我的 swift 项目中,我有一个使用协议(protocol)继承的案例,如下所示 protocol A : class{ } protocol B : A{ } 接下来我要实现的目标是声明另一个具
我想根据这两种协议(protocol)的一般特征(例如开销(数据包)、安全性、信息建模和可靠性)来比较 OPC UA 和 MQTT。我在哪里可以找到每个协议(protocol)的开销和其他特性的一些示
本质上,我的最终目标是拥有一个协议(protocol) Log,它强制所有符合它的对象都有一个符合另一个协议(protocol) [LogEvent] 的对象数组. 但是,符合Log的类需要有特定类型
我正在尝试为基于左操作数和右操作数标识的协议(protocol)实现 Equatable 协议(protocol)。换句话说:我如何为一个协议(protocol)实现 Equatable 协议(pro
问题不在于编程。 我正在使用一台旧机器,微软停止了这些机器的补丁。 有没有人针对攻击者已知的使用端口 445 的 SMB 协议(protocol)漏洞的解决方案? 任何棘手的解决方案? 换句话说,我想
在我们的业务中,我们需要记录到达我们服务器的每个请求/响应。 目前,我们使用 xml 作为标准实现。 如果我们需要调试/跟踪某些错误,则使用日志文件。 如果我们切换到 Protocol Buffer
你推荐什么协议(protocol)定义? 我评估了 Google 的 Protocol Buffer ,但它不允许我控制正在构建的数据包中字段的位置。我认为 Thrift 也是如此。我的要求是: 指定
我是一名优秀的程序员,十分优秀!