Created
July 20, 2018 06:47
-
-
Save mayooresan/bdff76c110a2e4b70e8d57a7b6dbeb3d to your computer and use it in GitHub Desktop.
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
//: 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