Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tgnivekucn/e7bbd26a91b741c1c9af9de548aeeef8 to your computer and use it in GitHub Desktop.
Save tgnivekucn/e7bbd26a91b741c1c9af9de548aeeef8 to your computer and use it in GitHub Desktop.
Factory pattern
// 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)
}
}
// Factory and Concrete Factory
protocol TrainingCamp {
func trainAdventurer() -> Adventurer
}
class ArcherTrainingCamp: TrainingCamp {
func trainAdventurer() -> Adventurer {
print("Train an archer")
return Archer()
}
}
class WarriorTrainingCamp: TrainingCamp {
func trainAdventurer() -> Adventurer {
print("Train an warrior")
return Warrior()
}
}
let archer = ArcherTrainingCamp().trainAdventurer()
let warrior = WarriorTrainingCamp().trainAdventurer()
// Testing code
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