Created
February 27, 2016 18:10
-
-
Save ericdke/d8dfa7f01f1a52feaa7c to your computer and use it in GitHub Desktop.
Delegation example #2 for the Aya.io tutorial.
This file contains hidden or 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
| import XCPlayground | |
| XCPlaygroundPage.currentPage.needsIndefiniteExecution = true | |
| protocol CanBuildHouses { | |
| func startWorking() | |
| } | |
| protocol CanManageTeams { | |
| func aTeamHasStartedWorking(team: Team) | |
| func aTeamHasFinishedWorking(team: Team) | |
| } | |
| class Contractor: CanManageTeams { | |
| var name: String | |
| init(name: String) { | |
| self.name = name | |
| } | |
| var employee: CanBuildHouses? | |
| func giveOrders() { | |
| guard let currentTeam = self.employee else { | |
| print("\(name): No new team in sight.\n") | |
| return | |
| } | |
| print("\(name): Ah! Let's give some work to this team...\n") | |
| currentTeam.startWorking() | |
| } | |
| func aTeamHasStartedWorking(team: Team) { | |
| print("\(name): Ok \(team.name), see you later today!\n") | |
| } | |
| func aTeamHasFinishedWorking(team: Team) { | |
| if team.taskDuration < 7 { | |
| print("\(name): Congratulations, \(team.name), work done in \(team.taskDuration) seconds.\n") | |
| } else { | |
| print("\(name): Really, \(team.name)? You can do better than \(team.taskDuration) seconds.\n") | |
| } | |
| } | |
| } | |
| class Team: CanBuildHouses { | |
| var name: String | |
| var reportsTo: CanManageTeams? | |
| var taskDuration:UInt32 = 0 | |
| init(name: String) { | |
| self.name = name | |
| } | |
| func startWorking() { | |
| if let announce = reportsTo { | |
| print("\(name): We're going to start in an instant, let's tell the boss...\n") | |
| announce.aTeamHasStartedWorking(self) | |
| } else { | |
| print("\(name): No boss in sight, let's just start working.\n") | |
| } | |
| dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { | |
| self.taskDuration = arc4random_uniform(10)+2 | |
| sleep(self.taskDuration) // "working" for some time | |
| dispatch_async(dispatch_get_main_queue()) { | |
| if let announce = self.reportsTo { | |
| print("\(self.name): We're finished, let's tell the boss!\n") | |
| announce.aTeamHasFinishedWorking(self) | |
| } else { | |
| print("\(self.name): We're finished! No boss in sight, let's just have a beer.\n") | |
| } | |
| } | |
| } | |
| } | |
| } | |
| let boss = Contractor(name: "BOSS") | |
| let teamJohn = Team(name: "JOHN") | |
| let teamJane = Team(name: "JANE") | |
| teamJohn.reportsTo = boss | |
| teamJane.reportsTo = boss | |
| boss.employee = teamJane | |
| boss.giveOrders() | |
| boss.employee = teamJohn | |
| boss.giveOrders() | |
| boss.employee = nil | |
| boss.giveOrders() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment