OBJECTIVE-C Protocol (Agreement) and delegate (delegate, agent)

Source: Internet
Author: User

First, what is the proxy mode?

Proxy mode is a kind of design pattern that is often encountered in OC, what is called proxy mode?

As an example:

There is a baby, he himself will not eat and bathe and so on some things, baby mother to work and very busy, so the baby mother asked for a nanny, so the baby mother and nanny agreed to an agreement, the agreement that the nanny needs to do something, and the nanny is the agent.

Second, the agent mode key points:

A do one thing, but you can not finish, so he find a agent B to do this for him, there is an agreement between them (Protocol,oc in the protocol is similar to Java interface, but it only declares the method), B inherits the protocol to complete a proxy to him things.

Third, case analysis

Here is a classic example of mom and nanny: Mother entrusted the child to the babysitter, so the need to complete the matter into a protocol: the agreement statement is as follows:

Works.h

1 #import<Foundation/Foundation.h>2 /**3 * Define some protocols for babysitting care for children4 * NSObject is the base protocol5  */6 @protocolWorks <NSObject>7 8 /**9 * Nanny Feeding AgreementTen  */ One-(void) feed; A  - /** - * Child Care Agreement the  */ --(void) Lookafter; -  - @end

Implements the protocol for nanny class header file LFNurse.h

1 #import<Foundation/Foundation.h>2 #import "Works.h"3 4 @interfaceLfnurse:nsobject <Works>//Agreement to implement babysitting care for children5 6@property (nonatomic,copy) NSString *Nursename;7 8-(Instancetype) Initwithname: (nsstring*) Nusername;9 Ten @end

Lfnurse.m

1 #import "LFNurse.h"2 3 @implementationLfnurse4 5-(Instancetype) Initwithname: (nsstring*) nusername{6     7     if(self =[Super Init]) {8_nursename =Nusername;9     }Ten     returnSelf ; One } A  --(void) feed{ -NSLog (@"The kid is hungry, give him feeding."); the } -  --(void) lookafter{ -NSLog (@"The kid's starting to get naughty, take care of him."); + } -  + @end

Define MOM header File LFMather.h

1 #import<Foundation/Foundation.h>2 #import "Works.h"3 @classLfnurse;4 5 @interfaceLfmather:nsobject6 7@property (Nonatomic,strong)ID<Works>Delegate;//agent, Mather work busy, need to find a nanny agent to take care of children8@property (nonatomic,copy) NSString *childname;//A child's name.9 Ten-(Instancetype) Initwithname: (nsstring*) name Deleagete: (ID<Works>)Delegate; One-(void) Delegatethingwithsomebody: (lfnurse*) nurse; A  - @end

Implementing File Lfmather.m

1 #import "LFMather.h"2 #import "LFNurse.h"3 4 5 @implementationLfmather6 7-(Instancetype) Initwithname: (nsstring*) name Deleagete: (ID<Works>)Delegate{8 9     if(self =[Super Init]) {TenSelf.childname =name; OneSelf.Delegate=Delegate; A     } -     returnSelf ; - } the  --(void) Delegatethingwithsomebody: (Lfnurse *) nurse{ -      -NSLog (@ "MatherChild's name:%@", self.childname); +[Self.Delegatefeed]; -[Self.DelegateLookafter]; +NSLog (@"Nanny's name:%@", nurse.nursename); A } at  - @end

The main function tests the code:

1 #import<Foundation/Foundation.h>2 #import "LFMather.h"3 #import "Works.h"4 #import "LFNurse.h"5 6 intMainintargcConst Char*argv[])7 {8 9 @autoreleasepool {Ten        OneNSString *name = [[NSString alloc] Initwithformat:@"Little fella?"]; A          -Lfnurse *nurseofdengzhiqi = [[Lfnurse alloc]initwithname:@"Deng Zi Chess"]; -Lfmather *mather =[[Lfmather alloc]initwithname:name Deleagete:nurseofdengzhiqi]; the          - [Mather Delegatethingwithsomebody:nurseofdengzhiqi]; -          -     } +     return 0; -}

Test results:

Iv. Summary:

In the agent, the key to remember is to declare the instance variable of Agent B in the class A that issued the proxy request, so that a can be guaranteed by invoking the B proxy in B to accomplish the thing of a being agent, that is, to explain (proxy) to B.

OBJECTIVE-C Protocol (Agreement) and delegate (delegate, agent)

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.