Delegate usage and explanation in objective-C

Source: Internet
Author: User

I first came into contact with the delegation when I learned about the big talk design model. I am a javaer. That book is. net, which mentioned the concept of delegation and had corresponding usage and support. At that time, I felt quite strange. Why didn't I encounter it in Java? Later, I learned that Java does not have explicit support, and I feel that delegation is very similar to dependency, aggregation, proxy, and event listening. So I haven't really understood it for a long time. I thought these are delegation, today, when I learned about the delegate in objective-C, I found that these are not. I carefully studied this and combined it with Java and finally got the definition of the delegate, although similar to the methods mentioned earlier, there are still some differences:

1) The employer and the entrusting party participate in a process. To put it bluntly, when calling the employer's method, not only does the employer's method have the corresponding logic, but also requests the entrusting party's method for joint processing.

2) When requesting the delegate's method, pass yourself over. That is to say, pass yourself as an input parameter. Of course, there can be different implementations, but the delegate can only obtain the employer.

In these two points, other concepts are distinguished at once, for example, none. However, one technology in Java is indeed a fully delegated application, that is, event listening. Remember swing's actionlistener, therefore, it is clear how the delegation is going. Of course, I will not talk about it here. I will think about it myself. Below is a demo of a simple delegate in objective-C, in advance, it is generally necessary to run reflection to be a real delegate, rather than a hard-coded fixed class like the one I wrote. I just want to explain the delegate, and, I am not afraid of everyone's laughter. I will not use objective-C's dynamic reflection. Many examples of Java delegation are on the Internet, so I will not write Java, the Code is as follows:

# Import <Cocoa/cocoa. h>

 

 

/**

* Delegated demo

*

* @ Author Wang Shuo

**/

 

// Delegate class. The delegate class does not have any method declaration. It only needs to implement the methods required in informal protocols.

@ Interface demodelegate: nsobject

 

@ End // demodelegate

 

// Employer type

@ Interface Demo: nsobject {

// Delegate class

Demodelegate * demodelegate;

}

 

-(Void) setdemodelegate: (demodelegate *) delegate;

 

-(Void) sendmessage;

 

@ End // demo

 

// Nsobject wsdelegate class, which defines informal protocols

@ Interface nsobject (wsdelegate)

 

-(Void) setdemodelegate: (demodelegate *) delegate;

 

-(Void) handlerdelegate: (Demo *) demo;

 

@ End // wsdelegate

 

@ Implementation demo

 

-(Void) setdemodelegate: (demodelegate *) delegate {

[Delegate retain];

[Demodelegate release];

Demodelegate = delegate;

}

 

// Notification delegate method execution

-(Void) sendmessage {

[Demodelegate handlerdelegate: Self];

}

 

-(Void) dealloc {

[Demodelegate release];

[Super dealloc];

}

 

@ End // demo

 

@ Implementation demodelegate

 

// Delegate Method

-(Void) handlerdelegate: (Demo *) demo {

Nslog (@ "% @", demo );

}

 

@ End // demodelegate

 

Int main (INT argc, char * argv []) {

 

NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];

Demo * demo = [[demo alloc] init];

Demodelegate * delegate = [[demodelegate alloc] init];

[Demo setdemodelegate: Delegate];

[Demo sendmessage];

[Pool release];

 

Return (0 );

 

}

 

Of course, the above Code comments are not particularly detailed, because I believe that if you are learning the objective-C delegate, you must have mastered the basic knowledge of objective-C, the above code is very simple. You can certainly understand it. These are the concepts I have summarized after the comprehensive information. If there is anything wrong, I hope to communicate with you more, I hope to help you.

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.