Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cmvandrevala/82a0123045ab2f1db205a0f65c7166c6 to your computer and use it in GitHub Desktop.
Save cmvandrevala/82a0123045ab2f1db205a0f65c7166c6 to your computer and use it in GitHub Desktop.
protocol Logger {
func takeAction(flag: Bool) -> Void
}
class LoggerA: Logger {
func takeAction(flag: Bool) {
print("LoggerA always prints this message.")
}
}
class LoggerB: Logger {
func takeAction(flag: Bool) {
if flag {
print("LoggerB, the flag is true!")
} else {
print("LoggerB, the flag is false!")
}
}
}
class FakeServer {
var loggerList: [Logger] = [LoggerA(), LoggerB()]
func run() {
someImportantAction()
notifyLoggers(flag: true)
anotherImportantAction()
notifyLoggers(flag: false)
}
private func someImportantAction() -> Void {
print("This is some important action!")
}
private func anotherImportantAction() -> Void {
print("This is another important action!")
}
private func notifyLoggers(flag: Bool) {
for item in loggerList {
item.takeAction(flag: flag)
}
}
}
var server = FakeServer()
server.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment