IOS proxy Mode
Proxy mode: one object allows another object to execute certain actions for it.
If you want to do one thing but do not want to do it by yourself, you need to create a proxy for the proxy to help you. It is embodied in the Code that declares a proxy object as a member variable. Call the method of the proxy object when you need to do something.
The following example shows that a resident needs to know the information about the movie pass to watch a movie, but the owner is very lazy and needs to ask the servant to help collect relevant information and tell the resident the result.
Person. h
@interface Person : NSObject@property (nonatomic,strong) Servant * delegate;- (void) buyTicket;@end
Person. m
@ Implementation Person-(void) buyTicket {NSLog (@ "% d remaining % d tickets for tickets with a fare of % d", [_ delegate ticketprice], [_ delegate leftTicketCount]);} @ end
Servant. h
@interface Servant : NSObject- (int) ticketprice;- (int) leftTicketCount;@end
Servant. m
@implementation Servant- (int)ticketprice{ //..... return 1;}- (int)leftTicketCount{ //..... return 1000;}@end
Main
int main(int argc, const char * argv[]){ Person *p = [[Person alloc] init]; Servant *s = [[Servant alloc]init]; p.delegate = s; [p buyTicket]; return 0;}
Print: there are still 1000 tickets with a fare of 1
The above code implements this function, but the problem is that the coupling between codes is too high. If the Servant class is changed, modify the content of the Person class accordingly. In iOS, proxy is implemented through the Protocol.
To reduce code coupling, first consider that the proxy Object Pointer declared in the Person class is a universal pointer instead of being limited to a class of a certain type, in this way, you do not need to change the content of this class to modify the proxy class.
That is, the following code Declaration
@property (nonatomic,strong) Servant * delegate;
Change
@property (nonatomic,strong) id delegate;
Obviously, this will cause an error in the method call in the Person. m class. Because the _ delegate member is of the id type, the compiler does not know what its methods are.
Therefore, you can extract the two methods into the protocol and set the protocol to be implemented when declaring the proxy variable. In this way, no errors cannot be returned, that is:
@property (nonatomic,strong) id
delegate;
Person. h
@interface Person : NSObject@property (nonatomic,strong) id
delegate;- (void) buyTicket;@end
TicketDelegate. h
@protocol TicketDelegate
@required- (int) ticketprice;- (int) leftTicketCount;@end
Servant1.h
// The Servant1 class implements the TicketDelegate Protocol @ interface Servant1: NSObject <TicketDelegate> @ end
Servant1.m
@implementation Servant1- (int)ticketprice{ //..... return 1;}- (int)leftTicketCount{ //..... return 1000;}@end
In this way, when you need to change the proxy, you do not need to modify the proxy object declaration in the Person class to reduce the coupling between codes. In addition, it seems that the proxy and callback are very similar at present, but the focus is different.