Delegate
A Delegate is an object that's given an opportunity to react to changes in another object or influence the behavior of another object. The basic idea is that two objects coordinate to solve a problem. One object is very general and intended for reuse in a wide variety of situations. It stores a reference to another object, its delegate, and sends messages to the delegate at critical times. The messages may just inform the delegate that something has happened, giving the delegate an opportunity to do extra processing, or the messages may ask the delegate for critical information that will control what happens. The delegate is typically a unique custom object within the Controller subsystme of your application.
Delegates highlight key advantages of using anonymous objects when designing reusable classes.
Motivation
Delegates simplify the customization of object behavior while minimizing coupling between objects. Cocoa's NSWindow class uses a delegate to control window behavior. So there is no need to subclass. The NSWindow class has a delegate and sends messages to the delegate just before the window is resized, closed, or otherwise modified. when the window's delegate receives messages sent by the window, the delegate can perform necessary application-specificprocessing such as checking to see if the window contains any unsaved chages before it is closed and if som giving the user a chance to save the changes or cancel the operation. The delegate can be part of the controller subsystem and have littel coupling to other subsystems. All the delegate has to do is implement the appropriate methods corresponding to messages that the window will send to it.
Delegate might be changed at runtime.
pp177 Figures of the benefits
Delegates eliminate one of the most common situations in which multiple-inheritance may be desired.
Solution
A delegate is an object referenced using Objective-C's anonymous type, id. The reference to the delegate is typically an instance variable named delegate, and mehods that use the Accessor pattern to set or return the current delegate object.
IBOutlet id delegate;
pp182
Tip
Naming Delegate Messages
There is a convention to the way messages sent to delegates are named. Each such message starts with an identification of the type of object that sends the message. The delegate messages sent by the MyBarView class all start with barView. Delegate messages usually include one of three verbs: should, will, or did.
Messages that use should are expected to return a value and usually take an argument that directly identifies the object sending the message. These messages are sent to the delegate before a change to the object sending the message. The delegate is given an opportunity to influence the change.
Message that use will are not expected to return values. Thers messages are sent before a change happens and are strictly informative. The delegate can implement a method to synchronize the application's state or perform additional processing when such a message is received.
Finally, the did messages are sent after a chage happens. These messages are also strictly informative and give the delegate an opportunity to perform processing after the change.
posted on 2013-02-26 01:06 Chansonyan 阅读(245) 评论(0) 收藏 举报
浙公网安备 33010602011771号