An agent for iOS common design patterns and mechanisms

Source: Internet
Author: User

Delegate:

1 use of proxy design patterns we need to understand the three elements first.

* Principal: The person who entrusted others to perform certain operations (object)

* Agent: The person (object) who performs certain operations on the delegated area

* Protocol: (protocol) The client needs the action of the agent

The > protocol is a set of criteria (a stack of method declarations), only. h files. The definition of the agreement begins with the @protocol and ends with the @end.

The methods in the > protocol must be implemented by default, @required. Keyword @optional adornment method is optional, class implementation can not be implemented.

2 Design steps for delegate

Step 1: The client formulates a set of agreements (in the. h file of the principal party), which declares the method that the principal needs to execute by the agent (only method declarations)

Step 2: The principal declares a delegate property (assign adornment) that stores the proxy object
@property (strong,nonatomic) id< protocol name >delegate

Step 3: The agent needs to follow the protocol and implement the methods in the Protocol

Step 4: Set the agent as the delegate's agent (to store the proxy object to the delegate property of the delegate object)

Step 5: The client notifies the proxy object at the appropriate time to perform the appropriate action

An agent for iOS common design patterns and mechanisms

Related Article

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.