Last active
February 1, 2016 17:20
-
-
Save eofster/b23749ec8133d50b2464 to your computer and use it in GitHub Desktop.
An example of using the observer pattern instead of NSnotifications and KVO
This file contains 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
class UserAgent { | |
let observer: UserAgentObserver | |
init(observer: UserAgentObserver) { | |
self.observer = observer | |
} | |
func start() { | |
observer.userAgentDidFinishStarting(self) | |
} | |
func stop() { | |
observer.userAgentDidFinishStopping(self) | |
} | |
} | |
protocol UserAgentObserver { | |
func userAgentDidFinishStarting(userAgent: UserAgent) | |
func userAgentDidFinishStopping(userAgent: UserAgent) | |
} | |
class UserAgentEventLogger: UserAgentObserver { | |
func userAgentDidFinishStarting(userAgent: UserAgent) { | |
print("User agent started") | |
} | |
func userAgentDidFinishStopping(userAgent: UserAgent) { | |
print("User agent stopped") | |
} | |
} | |
let userAgent = UserAgent(observer: UserAgentEventLogger()) | |
userAgent.start() | |
userAgent.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment