Swift Communication Patterns - Delegates, Notifications and Handlers




Communication Patterns play a major role in Application Development. Passing data between different classes are called Communication Patterns. So, during designing and implementing your project in iOS, can use the following communication methods.

The three communication patterns which are going to see here are,

1. Delegation
2. Notification
3. Closures/Blocks

Delegation
    iOS developers are extremely familiar with using delegates in a Swift codebase. In fact, almost every Apple framework is using delegates. Even the very first object that is responsible for doing a lot of stuff is a delegate i.e., UIApplicationDelegate.

"Delegates are a design pattern that allows one object to send messages to another object when a specific event happens."

Example:
      Think of delegates like a telephone call. You call up someone and specifically want to talk to them, You can say something, and they can respond. You can talk until you hang up the phone. Delegates, in much the same way, create and link between two objects, and you don't need to know what type the delegate will be, it simply has to implement the protocol.




Notification:
    It is a way for an object that initiates or handles a program event to communicate with any number of objects what want to know about the event. Notifications are also frequently used, its architectural pattern is used for many to many communication between objects, solving one of the limitations of delegates. "UIResponder.keyboardWillShowNotification" is a notification would like to be informed when the keyboard will be shown on the screen.

"A notification is a message sent to one or more observing objects to inform them of an event in a program"

Example:
   Notifications are like radio station. They broadcast their message to whoever is willing to listen. The radio station can't receive feedback from its listeners, the listeners can ignore the message or they can do something with it. Notifications allow you to send a message to any objects, but you won't have a link between them to communicate back and forth.



Closures/Blocks:
     In Swift Completion Handlers are closures, and they stated in Apple's documentation as

"Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages. ... Closures take one of three forms: Global functions are closures that have a name and do not capture any values."

Example:
  Common use of a closure expression comes in the form of a completion handler. If there is an operation that takes a relatively long time to complete i.e., say a network call or animation, you can use a completion closure to execute some code after that task is complete.

func animate(withDuration duration: TimeInterval, 
        animations: @escaping () -> Void, 
        completion: ((Bool) -> Void)? = nil)


Click here to check my next post on how to implement the delegation

Comments

Post a Comment