Skip to content

Instantly share code, notes, and snippets.

@mayooresan
Created July 20, 2018 06:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mayooresan/bdff76c110a2e4b70e8d57a7b6dbeb3d to your computer and use it in GitHub Desktop.
Save mayooresan/bdff76c110a2e4b70e8d57a7b6dbeb3d to your computer and use it in GitHub Desktop.
//: 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