Proxy, also known as a delegate, is a common design mode in iOS. As the name implies, it delegates what one object needs to do to another object. The other object acts as a proxy for this object, taking care of what it does. To reflect the program, we must first make clear which object is the delegate of an object and what the delegate does. In iOS programming, delegates are implemented by @protocol, so they are also called protocols. In the iOS SDK, UITableView, UITextField, etc., all use this design pattern. A protocol is a list of methods shared by multiple classes. The methods listed in the protocol have no responsive implementation and are implemented by other classes. Delegation refers to giving one object the opportunity to react to changes in another object or to act accordingly to another object. The basic idea is to solve problems cooperatively.

From the definition of methods, it is not difficult to see that the delegate mode can play two roles: first, the agent assists the object subject to complete an operation, and realizes the operations that need to be customized through the agent to achieve the same role as subclassing the object subject. Second: event listening, the proxy object listens to some important events of the object body, makes specific response to the event or broadcasts the event to the object that needs to respond.

In my understanding, the benefits of using the delegate pattern are: 1. Avoid excessive subclasses brought by subclassing and the coupling between subclasses and their parents; 2

1. The commission needs to do the following work:

1.1 Defining Protocols and Methods 1.2 Declaring delegate variables 1.3 Setting a Proxy 1.4 Invoking a delegate method through a delegate variableCopy the code

2. The agent needs to do the following:

2.1 Follow protocol 2.2 Implement delegate methodCopy the code

For example, students want to buy a professional book, the bookstore does not have this book, and they do not directly go to the press, so the students will entrust the bookstore to help buy books, the bookstore is the agent of students. Student. H inside:

#import <Foundation/Foundation.h>@protocol StudentBuyBookDelegate<NSObject> -(void)buyBook:(NSString *)name price:(int)p; @end@interface Student: NSObject @property(nonatomic,retain)id<StudentBuyBookDelegate> stu_delegate; -(void)wantBuy; @endCopy the code

Student. M inside:

Click here to join the group chat [iOS learning Exchange], which sorts out all kinds of interview questions, welcome to come to learn and exchange

#import "Student.h"

@implementation Student

-(void)wantBuy {
    
    NSLog(@"Student: I want to buy the IOS development book"); // Call the delegate method with the delegate variable [self.stu_delegate buyBook:@"IOS development" price:50];
}

@end
Copy the code

The bookstore BookShop. H

#import <Foundation/Foundation.h>
#import "Student.h"// The bookstore complies with the StudentBuyBookDelegate protocol. @interface BookShop: NSObject<StudentBuyBookDelegate> @endCopy the code

The bookstore BookShop. M

#import "BookShop.h"-(void)buyBook:(NSString *)name price:(int)p {NSLog(@)"I can sell you %@ for % I dollars.",p,name);
}

@end
Copy the code

In the ViewController. M

Student *student =[[Student alloc]init]; BookShop *bookshop = [[BookShop alloc]init]; Student.stu_delegate =bookshop; student.stu_delegate=bookshop; // Determine whether the bookstore implements the protocol to avoid a crash if it does notif ([bookshop respondsToSelector:@selector(buyBook:price:)])
    {
        [student wantBuy];
    }
Copy the code

The result is shown below: