Delegate and protocol

Source: Internet
Author: User

Protocols and proxies are really not flattering to a novice, and there are a lot of experienced iOS developers who are ignorant of this. A lot of blog posts on the internet just talk about how to use, and did not say very clearly. Let me talk a little bit about my understanding.

1. You need to understand why the protocol and the agent appear, that is, what the meaning of its existence, solve the problem.

Protocols and proxies are the product of modular development and encapsulation.
Let's start with a little story to help you understand:

Lao Wang has a restaurant, just beginning when the scale is very small, so Lao Wang a man did all the things: sweeping, cooking, welcome, serving, cashier. But with the enlargement of the scale, the old Wang one person will be unbearable, busy to die too busy to come. What happens now? Everyone is very clear, recruit! So there is a waiter, cashier, chef, cleaning clerk.
This meant that the old King's work was split by module.

The restaurant's workflow (business logic) is simple: food-------------------------clean the cashier.

The model that translates into the programming world is this: when the business is not very complicated, we write all the functions in a class, this class is called Lao Wang, in theory all things and functions can be written into this class. Cooking methods, serving methods, cleaning methods ... It caused the old Wang class to be very large and bloated, and prone to error.

Then we started hiring, new chef class, waiter class, cashier class, cleaning class, these four categories. Chef has cooking methods, waiter class order, serving method, cashier type has cashier method, cleaning class has cleaning methods.

This is still not possible, because module development will inevitably have module differentiation between the module communication problems. Chef class only Cook but what to do, must be timely serving, let customers enjoy. But the chef himself can not serve, so the chef must throw vegetables to do a good signal, specific this dish is not on, how on, it is not the chef care.

2. The role played by the agreement and the agency

Lao Wang confessed to the chef, you just do the dishes, the dishes are ready to shout a good meal (I have seen a restaurant is to pull the bell).
So the old king and chef's rule is the Protocol (Protocol), the following look at the code:

DaChu.h/** * The following is the fixed format of the Declaration protocol, Dachudelegate is the name of the protocol, because it is the proxy protocol, the name format is: class name +delegate */@protocol dachudelegate <nsobject>-(void) dosomethingaftercaizuohaole;  @end@interface dachu: nsobject/** * Delegate is a property of the Dachu class, and the weak keyword is to avoid circular references,<dachudelegate> To comply with the Dachudelegate agreement * more straightforward: In the chef's heart there is a person to accept his food good signal to do something, specific this person is who, chef does not care, this person's code is delegate */@property (  Nonatomic, weak) ID <DaChuDelegate> delegate;-(void) Kaishizuocai;  @end              
dachu.m #import  "DaChu.h"  @implementation dachu-(void) kaishizuocai{ Span class= "hljs-built_in" >nslog (@ "Start Cooking"); Sleep (2); nslog (@ "ready to serve the dishes"); //the following sentence is to determine whether delegate implemented Dosomethingaftercaizuohaole method, if delegate did not implement //Direct [self.delegate dosomethingaftercaizuohaole]; crash if ([self.delegate respondstoselector: @selector (dosomethingaftercaizuohaole )] {[self.delegate Dosomethingaftercaizuohaole];}}  @end             

Let's take a look at the contents of the Laowang class.

#import"LaoWang.h"#import"DaChu.h"@interfacelaowang () <dachudelegate>//< dachudelegate> means to comply with dachudelegate protocol, and implement the method inside the protocol  @end  @implementation  laowang-(void) laowangkaiye{nslog ( @ "Lao Wang opened"); Dachu *dachu1 = [[Dachu alloc] init]; Dachu1.delegate = self; It shows that Lao Wang acts as an agent and is responsible for receiving the signals of the dishes. [Dachu1 Kaishizuocai]; //chef starts cooking}-(void) dosomethingaftercaizuohaole{nslog (@" Lao Wang Knows ");  @end          

The above process can also seem to understand:

About the understanding of the agreement, you can also refer to this blog post.

Delegate and protocol

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.