An agent, also known as a principal agent (delegate), is a pattern commonly used in iOS. As the name implies, it is entrusted to other objects to do something to do. Then the other object is the agent of this object, instead of it to take care of what to do. Reflected in the program, the first thing to know is the object of the client is which object, the delegate is what the content. In iOS programming, delegates are implemented in a @protocol way, so they are called protocols. In iOS SDK, UITableView, Uitextfield and so on are useful to this design mode.
Protocol, which is a list of methods shared by multiple classes, is implemented by other classes that do not respond to the methods listed in the protocol. A delegate is a behavior that gives an object an opportunity to react to a change in another object, or a corresponding other object. The basic idea is to solve the problem together.
From the definition of the method, it is not difficult to see that the Commission model can play two roles:
First: The agent assists the object body to complete an operation, and it will need to customize the operation through the proxy to achieve the same effect as the sub-class object body.
Second: Event listener, the agent object listens to some important events of the object body, makes a specific response to the event or broadcasts the event to the object that needs to respond.
The advantages of a personal understanding of the use of a delegation model are:
1. Avoid the excessive subclasses and the coupling of subclasses and parent classes.
2. Hierarchical decoupling via delegated message mechanism
In the program: in general
1. The tasks to be commissioned are:
1.1 Defining protocols and methods
1.2 Declaring a delegate variable
1.3 Setting up Proxies
1.4 Calling a delegate method from a delegate variable
2. The work that the agent needs to do is:
2.1 Following the agreement
2.2 Implementing a Delegate method
For example, students want to buy a professional book, bookstore does not have this book, and do not go directly to the publishing house, so students commissioned a bookstore, to help buy books, bookstore is the student's agent.
Student Student.h Inside:
#import <foundation/foundation.h>//defines the protocol and method @protocol studentbuybookdelegate<nsobject>-(void) BuyBook: ( NSString *) name Price: (int) p; @end @interface student:nsobject//Declaration delegate Variable @property (nonatomic,retain) id< Studentbuybookdelegate> stu_delegate;-(void) wantbuy; @end
Student STUDENT.M Inside:
#import "Student.h" @implementation student-(void) wantbuy { NSLog (@ "Student: I want to buy an iOS development book"); Invoking a delegate method from a delegate variable [self.stu_delegate buybook:@ "ios development" price:50];} @end
Bookstore BookShop.h
#import <Foundation/Foundation.h> #import "Student.h"//Bookstore complies with Studentbuybookdelegate's entrustment agreement @interface Bookshop: Nsobject<studentbuybookdelegate> @end
Bookstore BOOKSHOP.M
#import "BookShop.h" @implementation the method of implementing the Protocol in BOOKSHOP//Bookstore-(void) Buybook: (NSString *) name Price: (int) p { NSLog (@ " I can sell%@ you ", P,name) with the price of%i yuan;} @end
Inside the VIEWCONTROLLER.M.
Student *student =[[student Alloc]init]; Bookshop *bookshop = [[Bookshop alloc]init]; The student sets up the agent, entrusts the bookstore to buy the book Student.stu_delegate=bookshop; Determine if the bookstore has implemented an agreement to avoid a crash if not implemented ([Bookshop respondstoselector: @selector (buybook:price:)]) { [student Wantbuy]; }
Results
Summary: A protocol is a list of method signatures in which several methods can be defined. Depending on the configuration, classes that adhere to this Protocol will implement several of the methods specified in this protocol. An agent is a concept that is difficult to define with a noun (if we can say that the agreement is actually a list of methods). It's more like a relationship, I'm going to do something, but I don't want to do it myself, I entrust someone else to do it for me. This time, the other person is my agent.
This article focuses on the concept of understanding, through the Student bookstore book Purchase example, explained the concept of Agent agreement. The following "iOS with Proxy interface reverse value" will focus on the application of Agent protocol in iOS programming, such as interface reverse value and so on.
IOS Proxy Protocol