Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//: Playground - noun: a place where people can play
import UIKit
protocol Human {
var name : String {get set}
func run()
func eat()
func sleep()
}
class Soldier : Human{
var name: String
init(SoldierName soldiername : String) {
self.name = soldiername
}
func run() {
print("soldier \(name) is running")
}
func eat() {
print("soldider \(name) is eating")
}
func sleep() {
print("soldider \(name) is sleeping")
}
}
class Civilian : Human {
var name: String
init(CivilianName civilianName : String) {
self.name = civilianName
}
func run() {
print("\(name) is running")
}
func eat() {
print("\(name) is eating")
}
func sleep() {
print("\(name) is sleeping")
}
}
enum HumanTypes{
case Soldier
case Civilian
}
class HumanFactory{
private static var sharedHumanFactory = HumanFactory()
class func shared() -> HumanFactory {
return sharedHumanFactory
}
func getHuman(HumanType humanType : HumanTypes, HumanName humanName : String)->Human{
switch humanType {
case .Soldier:
return Soldier(SoldierName: humanName)
case .Civilian:
return Civilian(CivilianName: humanName)
}
}
}
let soldier = HumanFactory.shared().getHuman(HumanType: .Soldier, HumanName: "Jay")
soldier.sleep()
let civilian = HumanFactory.shared().getHuman(HumanType: .Civilian, HumanName: "Saman")
civilian.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment