-
-
Save shmidt/08f10a5f675bcf8a3459878beaf8a971 to your computer and use it in GitHub Desktop.
Observer pattern with swift protocol extension
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
import Foundation | |
public protocol Observer: Equatable { | |
associatedtype EventType | |
func listen(_ event: EventType) | |
} | |
public protocol Observable { | |
associatedtype ObserverType: Observer | |
associatedtype EventType | |
var observers: [ObserverType] { get set } | |
mutating func addObserver(_ observer: ObserverType) | |
mutating func removeObserver(_ observer: ObserverType) | |
func notify(_ event: EventType) | |
} | |
extension Observable where EventType == ObserverType.EventType { | |
mutating public func addObserver(_ observer: ObserverType) { | |
var os = observers | |
os.append(observer) | |
observers = os | |
} | |
mutating public func removeObserver(_ observer: ObserverType) { | |
guard let i = observers.firstIndex(of: observer) else { return } | |
observers.remove(at: i) | |
} | |
public func notify(_ event: EventType) { | |
for o in observers { | |
o.listen(event) | |
} | |
} | |
} | |
public enum News { | |
case morning(String, Publisher) | |
case noon (String, Publisher) | |
case evening(String, Publisher) | |
case extra (String, Publisher) | |
} | |
public struct Subscriber: Observer, Equatable { | |
public typealias EventType = News | |
public var id: String | |
public var name: String | |
public func listen(_ event: EventType) { | |
switch event { | |
case .morning(let content, var publisher): read(content: content, publisher: &publisher) | |
case .noon (let content, var publisher): read(content: content, publisher: &publisher) | |
case .evening(let content, var publisher): read(content: content, publisher: &publisher) | |
case .extra (let content, var publisher): read(content: content, publisher: &publisher) | |
} | |
} | |
public func read(content: String, publisher: inout Publisher) { | |
print("\(name) reads morning paper: \(content)\n") | |
if content.contains(name) { | |
publisher.removeObserver(self) | |
print("\(name) unsubscribed\n") | |
} | |
} | |
} | |
public func ==(lhs: Subscriber, rhs: Subscriber) -> Bool { | |
return lhs.id == rhs.id | |
} | |
public class Publisher: Observable { | |
public var name: String | |
public typealias ObserverType = Subscriber | |
public typealias EventType = News | |
private var _observers: [ObserverType] = [] | |
public var observers: [ObserverType] { | |
get { return _observers } | |
set { _observers = newValue } | |
} | |
public init(name: String) { | |
self.name = name | |
} | |
} | |
var sentence_spring = Publisher(name: "Bunshun") | |
var kiyo = Subscriber(id: "kiyo" , name: "Kiyo") | |
var kimutaku = Subscriber(id: "kimura" , name: "Kimutaku") | |
var nakai = Subscriber(id: "nakai" , name: "Nakai") | |
var yoshiki = Subscriber(id: "yoshiki", name: "yoshiki") | |
sentence_spring.addObserver(kimutaku) | |
sentence_spring.addObserver(nakai) | |
sentence_spring.addObserver(kiyo) | |
sentence_spring.addObserver(yoshiki) | |
sentence_spring.notify(.morning("Becky and GESU NO KIWAMI are in inappropriate relationship!!!", sentence_spring)) | |
sentence_spring.removeObserver(nakai) | |
sentence_spring.removeObserver(kimutaku) | |
sentence_spring.notify(.evening("SMAP will be broken up!?" , sentence_spring)) | |
sentence_spring.notify(.extra ("Kiyo is arrested!!" , sentence_spring)) | |
sentence_spring.notify(.extra ("Sean K is pure japanease!!", sentence_spring)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment