Created
August 28, 2019 14:03
-
-
Save kmdarshan/d425061bba80394fe1dc7405c3efb074 to your computer and use it in GitHub Desktop.
Factory method implementation in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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