Skip to content

Commit

Permalink
added observer example
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Boldt committed Mar 16, 2018
1 parent 2a9831b commit e3afea1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//: [Previous](@previous)

import Foundation

protocol Observer : class {
func willChange(propertyName: String, newPropertyValue: Any?)
func didChange(propertyName: String, oldPropertyValue: Any?)
}

final class Observable {

weak var observer: Observer?
private let observablePropertyName = "observableProperty"

var observableProperty: Int = 0 {
willSet(newValue) {
observer?.willChange(propertyName: observablePropertyName, newPropertyValue: newValue)
}
didSet {
observer?.didChange(propertyName: observablePropertyName, oldPropertyValue: oldValue)
}
}
}

final class Spy : Observer {
func willChange(propertyName: String, newPropertyValue: Any?) {
print("will change to value \(newPropertyValue)")
}

func didChange(propertyName: String, oldPropertyValue: Any?) {
print("changed from oldValue \(oldPropertyValue)")
}
}

let observable = Observable()
let observer = Spy()
observable.observer = observer

observable.observableProperty = 200

//: [Next](@next)
2 changes: 2 additions & 0 deletions Behavioral.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
<page name='Iterator'/>
<page name='Strategy'/>
<page name='Mediator'/>
<page name='Observer'/>
<page name='Visitor'/>
</pages>
</playground>
8 changes: 8 additions & 0 deletions GOFSwift.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
* βœ… Strategy
* βœ… Mediator
* βœ… Visitor
* βœ… Observer
* ❌ Memento
* ❌ Interpreter
* ❌ Observer
* ❌ State

## Creational
Expand Down

0 comments on commit e3afea1

Please sign in to comment.