Skip to content

Instantly share code, notes, and snippets.

@LH17
Last active January 1, 2019 14:35
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 LH17/bcb797ad067b4bbb2935a9b4d0377233 to your computer and use it in GitHub Desktop.
Save LH17/bcb797ad067b4bbb2935a9b4d0377233 to your computer and use it in GitHub Desktop.
Chain Of Responsibility Design Pattern
enum Level: Int {
case state = 1
case national = 2
case international = 3
}
class Sports {
var level: Level
init(level: Level) {
self.level = level
}
}
protocol GameManagement {
var nextLevelManagement: GameManagement? { get set }
func manage(sports: Sports)
}
class StateSportsTeam: GameManagement {
var nextLevelManagement: GameManagement?
func manage(sports: Sports) {
if sports.level.rawValue == 1 {
print("Managed by State Sports Management")
} else {
nextLevelManagement?.manage(sports: sports)
}
}
}
class NationalSportsTeam: GameManagement {
var nextLevelManagement: GameManagement?
func manage(sports: Sports) {
if sports.level.rawValue == 2 {
print("Managed by National Sports Management")
} else {
nextLevelManagement?.manage(sports: sports)
}
}
}
class InternationalSportsTeam: GameManagement {
var nextLevelManagement: GameManagement?
func manage(sports: Sports) {
if sports.level.rawValue == 3 {
print("Managed by International Sports Management")
} else {
nextLevelManagement?.manage(sports: sports)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment