Skip to content

Instantly share code, notes, and snippets.

@cmvandrevala
Last active March 11, 2017 22:36
Show Gist options
  • Save cmvandrevala/ace57a97636d876519b68848cc01dbc3 to your computer and use it in GitHub Desktop.
Save cmvandrevala/ace57a97636d876519b68848cc01dbc3 to your computer and use it in GitHub Desktop.
protocol Logger {
func takeAction(flag: Bool) -> Void
func passDownChain(flag: Bool) -> Void
}
class LoggerA: Logger {
private var child: Logger?
public init(child: Logger?) {
self.child = child
}
func takeAction(flag: Bool) {
print("LoggerA will always print the same message")
passDownChain(flag: flag)
}
func passDownChain(flag: Bool) {
child?.takeAction(flag: flag)
}
}
class LoggerB: Logger {
private var child: Logger?
public init(child: Logger?) {
self.child = child
}
func takeAction(flag: Bool) {
if flag {
print("LoggerB prints this message if the flag is true.")
} else {
print("LoggerB prints this message if the flag is false.")
}
passDownChain(flag: flag)
}
func passDownChain(flag: Bool) {
child?.takeAction(flag: flag)
}
}
class FakeServer {
var logger = LoggerA(child: LoggerB(child: nil))
func run() {
someImportantAction()
logger.takeAction(flag: true)
anotherImportantAction()
logger.takeAction(flag: false)
}
private func someImportantAction() -> Void {
print("This is some important action!")
}
private func anotherImportantAction() -> Void {
print("This is another important action!")
}
}
var server = FakeServer()
server.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment