DEV Community

Binoy Vijayan
Binoy Vijayan

Posted on • Edited on

Efficient and Flexible Communication in Software Architecture: Swift custom implementation of NotificationCenter

In many software applications, efficient and flexible communication between various components is essential for maintaining a clean and maintainable architecture. Apple’s NSNotificationCenter is a widely used mechanism that facilitates such communication by allowing objects to post and observe notifications without needing to know about each other.

This article aims to guide you through the process of creating a custom notification center, similar to NSNotificationCenter, called MyNotificationCenter. We will explore its key features, including adding and removing observers, posting notifications, and ensuring thread safety. By the end of this guide, you will understand how to implement a robust and efficient notification system tailored to your application's unique needs.

Here are the components explained with the code snippet.

MyNotificationCenter

MyNotificationCenter is a singleton class designed to facilitate a custom notification center for broadcasting and receiving notifications within an application. It enables objects to register for specific notifications and handle them when posted.

Properties

static let shared: MyNotificationCenter

A shared singleton instance of MyNotificationCenter.

private var observers: [String: [(AnyObject, Selector)]]

A dictionary storing observers for each notification name, where the key is the notification name and the value is a list of tuples containing the observer object and its associated selector.

private let queue: DispatchQueue

A concurrent dispatch queue used to synchronise access to the observers dictionary.

Initializer

private init()

Initializes the observers dictionary and the concurrent dispatch queue. The initializer is private to enforce the singleton pattern.
Methods

func addObserver(object: AnyObject, name: String, selector: Selector)

Registers an observer for a specific notification.

Parameters:

object: The observer object.
name: The name of the notification.
selector: The selector to be called when the notification is posted.

func post(name: String, userInfo: [AnyHashable: Any]? = nil)

Posts a notification to all registered observers.

Parameters:
name: The name of the notification.
userInfo: An optional dictionary containing additional information about the notification.

func removeObserver(object: AnyObject)

Removes a specific observer from all notifications it is registered for.

Parameters:

object: The observer object to be removed.

MyNotificationObserver

MyNotificationObserver is a class that demonstrates how to use MyNotificationCenter to register for and handle notifications. It conforms to the Observable protocol.

Initializer

init()

Registers the instance as an observer for the "MyNotification" notification with MyNotificationCenter.

Methods

@objc func notificationSelector(_ userInfo: [AnyHashable: Any]?)

Handles the "MyNotification" notification when it is posted.

Parameters:

userInfo: An optional dictionary containing additional information about the notification.

Deinitializer

deinit

Removes the instance from the observers of "MyNotification" in MyNotificationCenter.

This documentation provides an overview of the MyNotificationCenter and MyNotificationObserver classes, explaining their purpose and how they interact to enable custom notification handling in your application.

Here is the complete source code for the same

Top comments (0)