Notification: Notification hubs are essentially a mechanism for providing message broadcasts within a program. Notification hubs cannot communicate between processes. is actually a setter that forwards the message to the desired object, based on the internal message forwarding. Notification hubs is based on the observer pattern, which allows you to register and delete observers.
A nsnotificationcenter can have a lot of notification messages nsnotification, for each nsnotification can have a lot of observers Observer to receive notifications.
Principal Agent: The principal agent (Degegate), as the name implies, is to delegate something to other objects to do. Then the other object is the agent of this object, instead of it to take care of what to do. Reflected in the program, the first thing to know is the object of the client is which object, the delegate is what the content.
Entrust mechanism is a design pattern, used in many languages, this is only a general idea, there will be a lot of information on the Internet.
Then the two in the process of development, exactly under what circumstances in which way more convenient and improve efficiency, according to the author's development experience summarized one or two, I hope you do not laughed at, there is a wrong place, welcome
Notice Advantage: The notification is very convenient to use, we only need to send messages where the message needs to be broadcast, as for the message inside how to send, we do not have to control, just want to get this message where we listen to, no matter how the message is here. In fact, the principle is not very complex, we are interested in the study can be their own.
The disadvantage of notification: If too many notifications, will cause the management of the notification complex, if the management is not good, you will receive the inexplicable message, and can not be traced.
Agent advantages: First, the efficiency of the agent is higher than the notification (personal opinion), in fact, the agent is the C + + callback, call We are very clear, plus a back may be a lot of people feel confused, in fact, is also called, just in our intuitive look, it feels like I have done something, But we can still receive the feedback that has been in the past, in fact, this notice is similar, but we ourselves in what has been done to pre-judge something may happen, and then actively tell us, we want to get something.
Agent disadvantage: The implementation is more complex, and the application of the scene is not much notice.
So below we discuss under what circumstances with the agent, under what circumstances with notice:
If the sender of a notification has multiple recipients, and the accepted location is completely uncertain, then it is better to use a notification in this case.
If a class can get to the object of the notification, in this case, we use the agent more efficient, and can better implement the management of the object to be proxied.
Application Scenarios for proxies:
N Object A something happened inside, want to notify object B
N object B wants to listen to what's going on inside object A.
N Object A wants to invoke a method of object B within its own method, and object A cannot have a coupling dependency on object B
N Object A wants to pass data to object B
The result is the same: Object B is the proxy for object A (delegate)
N Common
Communication between objects can be accomplished with notifications and proxies
(e.g. a object tells the B object what's going on, a object passes data to the D object)
N Different points
Proxy: One-to-two (an object can only tell the other 1 objects what's going on)
Notification: A Many-to-many relationship (an object can tell what happened to n objects, and 1 objects know what happened to n objects)
KVO
KVO is an object that can observe the property values of another object and be able to discover changes in values. KVO is more suitable for any type of object to listen for changes to another arbitrary object, or a way to keep an object in sync with another object, that is, when the state of another object changes, the observer responds immediately. It can only be used to react to a property, not to respond to a method or action.
Advantages of KVO:
1, can provide a simple method to achieve the synchronization between two objects;
2, can respond to non-created objects, that is, the state of the internal objects, and do not need to change the internal object (SDK object) implementation;
3, the ability to obtain the latest values of the observed properties and previous values;
4, with the key path to observe the attributes, so you can also observe the nested object (that is, you can observe the changes in the properties of an object's internal objects, can be infinite nesting observation, if the object's attributes support Kvo);
5. The abstract of the observed object is completed, because no additional code is required to allow the observed value to be observed (no need to send notifications like notifications, KVO property changes, external can be directly observed).
KVO Precautions:
When we register KVO, we want to observe which property, when calling the registration method, AddObserver:forKey:options:context:forKey The property is filled in string form, in case the property name is wrong, because it is a string, The compiler also does not warn and check
Use of KVO:
The addObserver:forKey:options:context is emitted by the Observer: method to add the observer. Then as long as the keypath value of the Observer changes (note: Simply changing its value will not call this method, only by getters and setters to change the value will trigger KVO), will be called in The Observer Observevalueforkeypath:ofobject : Change:context: So the observer needs to implement the method ObserveValueForKeyPath:ofObject:change:context; To respond to the notice given by KVO.
The code only needs to be implemented in the viewer, and the observer does not have to add any code, so who listens to whom to register and then processes the response so that the observer is fully decoupled from the observer, the use is flexible and simple, but the KVO can only detect attributes in the class, and the property name is NSString To find, the compiler will not help you to check the wrong and complete, pure hand knocking so more prone to error.
IOS------Notifications, proxies, KVO detailed