Skip to content

Instantly share code, notes, and snippets.

@kmdarshan
Created August 28, 2019 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmdarshan/d425061bba80394fe1dc7405c3efb074 to your computer and use it in GitHub Desktop.
Save kmdarshan/d425061bba80394fe1dc7405c3efb074 to your computer and use it in GitHub Desktop.
Factory method implementation in Swift
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
enum Maps : Int {
case google = 1
case apple
}
protocol Display {
func showMap()
}
class Map {
let type : Int = 0
func showMap(type : Maps) -> Display {
switch type {
case Maps.apple :
return AppleMap()
case Maps.google :
fallthrough
default:
return GoogleMap()
}
}
}
class AppleMap : Display {
func showMap() {
print("showing apple map")
}
}
class GoogleMap : Display {
func showMap() {
print("showing google map")
}
}
class MyViewController : UIViewController {
override func loadView() {
let 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)
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment