Own translation delegation official documents

Source: Internet
Author: User

What is an agent, know how to use, have seen n times. It is not enough to use proxies to pass values. What is the agent used to do? or look at the official documents, translate them to see if they are fluent


A proxy is a simple and efficient pattern, especially when a class represents or needs to coordinate with another class during programming. The delegate object (the principal party) holds a reference to the proxy object (the agent) and sends a message to the agent at the appropriate time. This message informs the agent that the principal will be dealing with one thing or just finishing up an event. The agent may respond to this message, such as updating the interface or State of itself or another class, and in some cases, the agent can return a value that affects an event that is about to be processed. The main value of the agent is that it allows you to easily customize the behavior of multiple classes in a central class.

Proxy and Cocoa framework

A delegate object is typically a framework object, and a proxy object is typically a custom controller object. In a managed memory environment, the delegate object holds a weak reference to its own proxy object, and in a garbage collected environment, the recipient [1] holds a strong reference to his agent. Examples of proxies are found everywhere in the foundation framework, Uikit,appkit, and other Cocoa,cocoa touch frameworks.

To cite an example of a principal: in the AppKit framework, there is an instance of the Nswindow class, Nswindow in his method Windowshouldclose: declares a protocol, when the user clicks the Close window button, This window object will send the Windowshouldclose: method to his proxy object to ask if it wants to confirm closing the window. The proxy returns a Boolean value to control the behavior of the Window object.

Agents and Notifications

In the class of most cocoa frameworks, the agent is automatically registered as the observer for the notification sent by the principal. The agent only needs to implement a notification method that is declared by the framework class to accept this special notification message. As shown in the example above, a window class emits a NSWINDOWWILLCLOSENOTIFICATION message to the observer, but sends a windowshouldclose: message to its agent.

Data source

A data source is almost the same as a proxy. The difference lies in the relationship with the principal. Unlike the controller of the user interface, a data source is proxied by the data controller. A delegate object is typically a view object, such as a tableview (table view), which typically holds a reference to his data source and often requests data to be presented to the data source. A data source, like care, must adopt a protocol and at least implement a method that the agent must implement. The data source is responsible for managing the model objects so that the model objects are routed to the delegate view.

Original address:


Own translation delegation official documents

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.