Lightweight notification center implementation for Swift using observer pattern.
- β¨ Simple observer registration and removal
- π’ Event broadcasting to multiple observers
- π§Ή Automatic memory management
- β‘ Lightweight and fast
// Add observer
CustomNotificatinCenter.shared.addObserver(observer: self, name: "moneyReceived") {
print("Money received!")
}
// Post notification
CustomNotificatinCenter.shared.post(name: "moneyReceived")
// Remove observer
CustomNotificatinCenter.shared.removeObserver(observer: self, name: "moneyReceived")π’ First announcement - all companies are subscribed:
Lidership: we got more money!
Audi: Give money to Audi, we will make new electric audi!
Porsche: Just give money to Porsche
Volkswagen: Give money to Volkswagen, we will make cheap car for people!
π° Porsche receives money and unsubscribes:
Porsche received money!
π’ Second announcement - Porsche should NOT respond:
Lidership: we got more money!
Audi: Give money to Audi, we will make new electric audi!
Volkswagen: Give money to Volkswagen, we will make cheap car for people!
π° Audi receives money and unsubscribes:
Audi received money!
π’ Third announcement - only Volkswagen should respond:
Lidership: we got more money!
Volkswagen: Give money to Volkswagen, we will make cheap car for people!
β¨ Demonstration: Notice how after each company receives money and unsubscribes, they stop responding to future announcements. This shows the notification center correctly removes observers.
CustomNotificatinCenter.swift- Main notification center implementationCarConcern.swift- Car companies observer examplemain.swift- Demo application