Delegate, NOTIFICATION,KVO pros and cons?

Source: Internet
Author: User
Tags notification center

Usually in the development, often encounter different classes to communicate between, we usually have the following options:

1.delegate

2.Notification

3.KVO

Features are as follows:

Delegate

1. Strict syntax, clear definition. such as the definition of a protocol implementation.

2. Logic is clear, control process can be tracked and identified.

3. Multiple protocols can be defined in a class, each of which corresponds to a different delegate.

4. Define more code, such as protocol, delegate attribute, implementation of proxy method.

5. One-to-one communication.

Notification

1. There is no need to write much code, the implementation is relatively simple.

2. An object sends a notification that multiple objects can react, and a one-to-many approach to implementation is straightforward.

3. In the compilation period will not check whether the notification can be properly handled by the observer;

4. When releasing the registered object, you need to cancel the registration in the Notification Center;

5. In the commissioning of the application of the work and control process difficult to track;

6. Third-party objects are required to manage the connection between the Controller and the Observer object;

7.controller and observers need to know the notification name, UserInfo dictionary keys in advance. If these are not defined in the work interval, then there is an unsynchronized situation;

8. After the notification is issued, the controller cannot obtain any feedback from the observer (compared to delegate).

KVO :

1. The ability to provide a simple way to achieve synchronization between two objects. Example: synchronization between model and view;

2. be able to respond to changes in the state of objects not created by us, i.e. internal objects, without changing the implementation of the inner object (SKD object);

3. ability to provide the latest value of the observed properties and previous values;

4. use key paths to observe attributes, so you can also observe nested objects;

5. The abstraction of the observed object is completed because no additional code is required to allow the observed value to be observed

6. can be a pair of more.

7. the attributes we observe must be defined using strings . Therefore, the compiler will not appear warning and check;

8. Refactoring the property will cause our observation code to be no longer available;

9. a complex "IF" statement requires that the object is observing multiple values. This is because all the observation code is directed by a method;

The observer is not required to be removed when the observer is released.

According to the above characteristics, we can see the difference between them

1.delegate is more efficient and more direct than NSNOTIFICATION/KVO.

2, KVO and Nsnotification both are responsible for giving notice, the rest of the matter, no return value, delegate can have a return value.

3.delegate is just one-to-one, KVO and nsnotification can be a couple.

Summary: When dealing with the event of the property layer message, use KVO, the other try to use delegate, if the code is really simple to deal with, then it would be easier to use nsnotification.

Delegate, Notification,kvo pros and cons?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.