Protocol and proxy, protocol proxy

Source: Internet
Author: User

Protocol and proxy, protocol proxy

======================================

Protocols and Proxies

======================================

[Agreement]

@ Protocol

 

[Role of the Protocol]: Specifies the interface method to be implemented. An agreement that must be followed by the entrusting party and the agent.

Delegate by (nt. delegate = self;) [Note: you can delegate yourself or other classes]

The proxy uses ([_ delegate test];) to perform the method proxy (....)

 

[Example ]:

James is the entrusting party and Xiaogang is the agent. They all follow an agreement. The Protocol sets two methods (functions). James delegated himself to Xiaogang and Xiaogang held a pointer to Xiaoming's object, through this pointer, James can access any method (it must be the method specified in the Protocol)

[Note] the methods in the Protocol have the following 2 permissions:

1. @ required // The delegate (James) must implement this method

2. @ optional // The delegate (James) can implement this method or not.

 

[Note] If the method required by @ required is not implemented, a warning is displayed. If @ optional is not implemented, there is no problem.

[Note] Which protocol is used must contain the header file of the Protocol.

 

[Proxy and Protocol addition method]

1. include header files

2. Add the protocol to the entrusting party and then implement the Protocol method.

3. Implement proxy object variables on the agent side.

4. assign values to proxy object variables on the delegate

5. Access the delegate method through the variables of the proxy object as needed

 

# Import <Foundation/Foundation. h>

# Import "xiaoming. h"

Int main (int argc, const char * argv []) {

@ Autoreleasepool {

Xiaoming * xm = [[xiaoming alloc] init];

// Used for testing

[Xm method];

}

Return 0;

}

 

# Import <Foundation/Foundation. h>

# Import "print. h"

# Import "xiaoming. h"

# Import "xiaogang. h"

@ Implementation xiaoming

// Practice delegate and proxy usage in method

-(Void) method

{

Xiaogang * xg = [[xiaogang alloc] init];

// Delegate yourself to Xiaogang. This is a process of assigning values.

Xg. woshidaili = self;

// Test whether the agent is easy to use

[Xg test];

}

-(Void) OutPutPersonInfo

{

NSLog (@ "I am the entrusting party and I entrust Xiaogang to do one thing ");

}

@ End

 

# Import <Foundation/Foundation. h>

# Import "print. h"

@ Interface xiaoming: NSObject <print>

-(Void) method;

@ End

 

# Import <Foundation/Foundation. h>

# Import "print. h"

@ Interface xiaogang: NSObject

// Woshidaili is a custom type with the id <print>

// It can be understood as: id type, but this id type complies with the Protocol

// [Note] the angle brackets after the id must follow the protocol name

@ Property id <print> woshidaili;

// Used to test whether the agent is easy to use

-(Void) test;

@ End

 

# Import "xiaogang. h"

@ Implementation xiaogang

-(Void) test

{

// Access the delegate's method through the variable of the proxy object

[_ Woshidaili OutPutPersonInfo];

}

@ End

 

# Import <Foundation/Foundation. h>

@ Protocol print <NSObject>

// Print personal information

-(Void) OutPutPersonInfo;

@ End

[Protocol and proxy usage]

For example, if you have purchased a product on the product page but you have not logged on to the product page, You need to log on first. After logging on to the product page, you need to tell the product page the status of the product page to determine the login status, and then proceed to the next step. [Note] the login page is an object created on the product page. [proxy callback]

[Protocol and proxy restrictions]

Data can only be accessed between two associated classes.

[Example] What does Xiao Gang do with Xiao Gang? What does Xiao Gang do with Xiao Hong?

[RespondsToSelector method]

Role: makes the code safer and prevents crashes

// Determine whether the OutPutPersonInfo method exists and the Skip method exists.

If ([_ woshidaili respondsToSelector: @ selector (OutPutPersonInfo :)])

{

[_ Woshidaili OutPutPersonInfo: @ "I am Xiaogang"];

}

 

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.