Skip to content

Instantly share code, notes, and snippets.

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 tgnivekucn/15a421f7b757c6d43fbba1fa930197e6 to your computer and use it in GitHub Desktop.
Save tgnivekucn/15a421f7b757c6d43fbba1fa930197e6 to your computer and use it in GitHub Desktop.
Simple Factory
// Product and Concrete Product
protocol Adventurer {
func getType() -> String
}
class Archer: Adventurer {
func getType() -> String {
print("I'm an archer")
return String(describing: Archer.self)
}
}
class Warrior: Adventurer {
func getType() -> String {
print("I'm a warrior")
return String(describing: Warrior.self)
}
}
// Simple Factory
class TrainingCamp {
static func trainAdventurer(type: String) -> Adventurer? {
switch type {
case "archer":
return Archer()
case "warrior":
return Warrior()
default:
return nil
}
}
}
let archer = TrainingCamp.trainAdventurer(type: "archer")
let warrior = TrainingCamp.trainAdventurer(type: "warrior")
assert(archer is Archer, "Didn't get the archer object!!")
assert(warrior is Warrior, "Didn't get the warrior object!!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment