IOS proxy protocol and ios proxy Protocol

Source: Internet
Author: User

IOS proxy protocol and ios proxy Protocol

Proxy, also known as the delegate proxy, is a common design mode in iOS. As the name suggests, it delegates the tasks of an object to other objects. So other objects are the proxies of this object, instead of them to take care of what to do. To be reflected in a program, you must first specify the object that the principal of an object is and what the principal is doing. In iOS programming, delegation is implemented through a @ protocol method, which is also called a protocol. In the iOS SDK, UITableView and UITextField are all used in this design mode.

Protocol is a list of methods shared by multiple classes. The methods listed in the Protocol do not implement the response, and are implemented by other classes. A delegate is an action that gives an object the opportunity to respond to changes in another object or to another object. The basic idea is to solve problems collaboratively.

 

From the definition of methods, we can easily see that the delegated mode can play two roles:

First, the proxy assists the object subject in completing an operation, and customizes the operation through the proxy customization to achieve the same effect as the subclass object subject.
Second, event listening: the proxy object listens to some important events of the object subject, and makes a specific response to the event or broadcasts the event to the objects needing a response.


The advantage of the delegated mode is:
1. Avoid the excessive subclass caused by subclass and the coupling between subclass and parent class.
2. Hierarchical decoupling through the message delivery mechanism

 

 

In the program: Generally

1. The tasks to be commissioned include:

1.1 define protocols and Methods

1.2 declare delegate Variables

1.3 set proxy

1.4 call the delegate method using the delegate variable

2. the proxy needs to do the following:

2.1 follow the agreement

2.2 Delegate Method

 

For example, if a student wants to buy a professional book, And the bookstore does not have this book, he does not directly go to the publishing house. Therefore, the student entrusts the bookstore to help buy the book. The bookstore is the student's agent.

Student. h:

# Import <Foundation/Foundation. h> // define the protocol and method @ protocol StudentBuyBookDelegate <NSObject>-(void) buyBook :( NSString *) name price :( int) p; @ end @ interface Student: NSObject // declare the delegate variable @ property (nonatomic, retain) id <StudentBuyBookDelegate> stu_delegate;-(void) wantBuy; @ end

Student. m:

# Import "Student. h "@ implementation Student-(void) wantBuy {NSLog (@" Student: I want to buy a book for IOS development "); // call the delegate method [self. stu_delegate buyBook: @ "IOS development" price: 50] ;}@ end

 

BookShop. h

# Import <Foundation/Foundation. h> # import "Student. h" // The bookstore complies with the delegation protocol of StudentBuyBookDelegate @ interface BookShop: NSObject <StudentBuyBookDelegate> @ end

 

BookShop. m

# Import "BookShop. h "@ implementation BookShop // method for implementing the bookstore protocol-(void) buyBook :( NSString *) name price :( int) p {NSLog (@ "I can sell you % @ at the price of % I RMB", p, name) ;}@ end

 

In ViewController. m

Student * student = [[Student alloc] init]; BookShop * bookshop = [[BookShop alloc] init]; // students set up agents and entrust bookstores to buy student books. stu_delegate = bookshop; // determine whether the bookstore has implemented the Protocol to avoid the failure if ([bookshop respondsToSelector: @ selector (buyBook: price :)]) {[student wantBuy];}

 

Result

 

Conclusion: The Protocol is a list of method signatures in which several methods can be defined. According to the configuration, the class complying with this protocol will implement several methods specified in this Protocol. Proxy is a concept and it is difficult to define it using a noun (for example, we can say that a protocol is actually a list of methods ). It is more like a relationship. I want to do something, but I don't want to do it myself. I entrust others to help me with it. At this time, this other person is my proxy.

This article focuses on the concept and explains the concept of agency agreement through the example of purchasing books in the student bookstore. The following sections focus on the application of the proxy protocol in iOS programming, such as reverse value transfer on the interface.

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.