Proxy extension-implicit proxy and multicast proxy, proxy Extension

Source: Internet
Author: User

Proxy extension-implicit proxy and multicast proxy, proxy Extension

I really don't want to talk about proxy. It is estimated that iOS developers can use it skillfully. Like Notification and Block, they are all familiar with it.

Here is a little talk about proxy extensions: Implicit proxy and multicast proxy are actually very simple.

 

Implicit Proxy: you do not need to comply with the Protocol when defining the Protocol attributes, and the classes that implement the methods do not need to comply with the Protocol, because the Protocol methods are defined in the NSObject classification.

Directly add the code, which is really very simple:

1. header file of the Person class:

1 # import <Foundation/Foundation. h> A classification of 2 3/** 4 NSObject, which declares the (proxy) Method 5 */6 @ interface NSObject (myfenlei) 7 8-(void) eat; 9 10 @ end11 12/** 13 define a class 14 */15 @ interface Person: NSObject16 17 @ property (nonatomic, copy) NSString * name; 18 19/** 20 proxy attributes, do not respect the protocol, because the NSObject classification method is implemented 21 */22 @ property (nonatomic, weak) id delegate; 23 24-(void) run; 25 26 @ end

2. The. m file of the Person class:

1 # import "Person. h "2 3 @ implementation Person 4 5-(void) run {6 NSLog (@" % @ running ", self. name); 7 8 dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (2 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {9 if ([self. delegate respondsToSelector: @ selector (eat)]) {10 // call the eat method of the proxy object (declared in the NSObject category) 11 [self. delegate eat]; 12} 13}); 14} 15 16 @ end

3. Call the run method of the Person object in the Controller. Call the proxy method in the run method:

1 # import "ViewController. h "2 # import" Person. h "3 4 @ interface ViewController () 5 6 @ end 7 8 @ implementation ViewController {9 Person * _ per; 10} 11 12-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {13 _ per = [[Person alloc] init]; 14 _ per. name = @ "James"; 15 _ per. delegate = self; // specify the proxy 16 [_ per run]; // call the method, in the method, call the proxy method 17} 18 19/** 20 to implement the Protocol Method 21 */22-(void) eat {23 NSLog (@ "when it's finished, prepare for dinner "); 24} 25 26 @ end

It's done. No more. Implicit proxy is so simple. The most important thing is that the Protocol method is defined in the NSObject classification. Is it easy to understand this point.

 

Next: Let's talk about multicast proxy.

Multicast proxy and notification are similar. One object sends messages and multiple object listeners. But better than notifications: 1. The notification Key is easy to write incorrectly; 2. The notification logic is unclear.

Idea: it is actually to define a proxy attribute, but this proxy attribute is only an array, and then provide an external method to add elements to this array. Then, when the object method is called, you can traverse this attribute cyclically (array type. It is easy to understand this point.

After talking so much, I am confused about myself. It is better to directly access the code. There are very few codes, so I will not explain them.

1. Person header files:

1 # import <Foundation/Foundation. h> 2 3/** 4 define the protocol and the declaration method is 5 */6 @ protocol PersonDelegate <NSObject> 7 8-(void) eatSomething; 9 10 @ end11 12 13 @ interface Person: NSObject14 15/** 16 Add proxy method 17 18 @ param delegate <# delegate description #> 19 */20-(void) addDelegate :( id) delegate; 21 22-(void) eat; 23 24 @ end

2. Person class. m file:

1 # import "Person. h "2 3 @ interface Person () 4 5/** 6 proxy member variable, is an array of 7 */8 @ property (nonatomic, strong) NSMutableArray * delegates; 9 10 @ end11 12 @ implementation Person13 14 // Add the element 15 to the proxy array (void) addDelegate :( id) delegate {16 [self. delegates addObject: delegate]; 17} 18 19 // key method: traverses the array when calling the object method. The current method is 20-(void) eat {21 for (id delegate in self. delegates) {22 [(id) delegate eatSomething]; 23} 24} 25 26 // lazy loading, initialize proxy attribute array 27-(NSMutableArray *) delegates {28 if (_ delegates = nil) {29 _ delegates = [NSMutableArray array]; 30} 31 return _ delegates; 32} 33 34 @ end

3. Now, let's take a look at how to use it:

1 OneViewController * oneVc = [[OneViewController alloc] init]; 2 TwoViewController * twoVc = [[TwoViewController alloc] init]; 3 4 Person * per = [Person new]; 5 6 // set multicast proxy 7 [per addDelegate: oneVc]; 8 [per addDelegate: twoVc]; 9 10 // call proxy method 11 [per eat];

4. You can see that when you call a method to add elements to the proxy attribute (array) and then call the eat method, traverse the array and execute the method. Here

Both OneViewController and TwoViewController implement the-(void) eatSomething; Method in the Protocol.
 1 // OneViewController 2 #import "OneViewController.h" 3  4 @interface OneViewController () 5  6 @end 7  8 @implementation OneViewController 9 10 - (void)eatSomething{11     12     NSLog(@"one");13 }14 15 @end16 17 // TwoViewController.18 #import "TwoViewController.h"19 20 @interface TwoViewController ()21 22 @end23 24 @implementation TwoViewController25 26 - (void)eatSomething{27     28     NSLog(@"two");29 }30 31 @end

Okay, that's it. Isn't it easy.

 

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.