Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nsnotification or delegation?

I just researched the two, and they both seem to be great means of communication, but nsnotification seems a ton easier to deal with. What are situations where you would want to use delegation rather than nsnotification, and nsnotification rather than delegation?

like image 294
Mindi Hansen Mende Avatar asked Nov 13 '12 04:11

Mindi Hansen Mende


1 Answers

I typically use delegation between classes within a subsystem, but use notifications across subsystem boundaries that don't need to be directly linked together and where strict order of notification isn't important. I also use Delegation when a class needs something done on its behalf directly (like UITableView does) and Notifications for when actions must occur not directly on behalf of the notifier, but for their own purpose.

Notifications are good for loosely coupled systems, like for instance when you finish logging in to a server and a bunch of other subsystems need to know its ok to proceed with whatever their tasks should be doing. These systems may not all need to have a direct relationship with your login subsystem, but they all need to do 'something' as a result. Notifications also do not dictate the form of the implementation as Delegates do, because the notifier shouldn't care.

Delegates are good in a more tighter coupled system where the implementation can be dictated (like saying 'now do this, and this, and then that'), typically by implementation of a protocol to which the delegate must adhere. Delegation is also easier to follow if you are trying understand how various pieces of code work together because the relationship are more clearly identified.

Here's a good post on this subject as well.

like image 53
foggzilla Avatar answered Oct 17 '22 00:10

foggzilla