Swift 3.0中工厂设计模式的实现

问题描述 投票:0回答:1

我想在Swift 3.0中实现工厂设计模式

我正在考虑的基本解决方案是:

这是一种合理的方法吗?

或者Swift中是否有其他设计模式?

ios swift3 protocols
1个回答
0
投票

您可以尝试使用此实现作为参考。https://redflowerinc.com/implementing-factory-design-pattern-in-swift/

导入UIKit导入PlaygroundSupport

枚举地图:Int {情况下google = 1案例苹果}

协议显示{func showMap()}

类地图{令类型:整数= 0func showMap(type:Maps)->显示{开关类型{案例Maps.apple:返回AppleMap()案例Maps.google:失败默认:返回GoogleMap()}}}

AppleMap类:显示{func showMap(){打印(“显示苹果地图”)}}

GoogleMap类:显示{func showMap(){打印(“显示Google地图”)}}

class MyViewController:UIViewController {覆盖func loadView(){让view = UIView()view.backgroundColor = .white

    let label = UILabel()
    label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
    label.text = "Hello World!"
    label.textColor = .black

    view.addSubview(label)
    self.view = view

    let map = Map()
    map.showMap(type: Maps.google)
    map.showMap(type: Maps.apple)
}

}

© www.soinside.com 2019 - 2024. All rights reserved.