objective-c Proxy Mode (delegate)One, what is the proxy mode? Proxy mode is a kind of design pattern that is often encountered in OC, what is called proxy mode? For example: There is a baby, he himself does not eat and bathe and so on some things, so the baby has a nanny, so the baby and the nanny agreed to a deal, the agreement that the nanny needs to do something, and the nanny is the agent, namely: Baby and Nanny There is an agreement between the nanny, the agreement, The nanny would then need to implement the terms of the agreement as an agent. Second, the agent mode key point: A to complete one thing, but oneself can not complete, so he find a agent B for him to complete this thing, they have an agreement (protocol), B inherit the agreement to complete a agent to him. Third, example analysis below to give a classic example, Mom and nanny examples: Mother entrusted the child to the nanny care, so the need to complete the matter into a protocol: the agreement statement is as follows:
#import <Foundation/Foundation.h>
@protocol Job <NSObject>
-(void) takeeat;
-(void) takesleep;
-(void) takeplay;
-(void) takeshower;
@end
We declare that the nurse class is the agent:
#import <Foundation/Foundation.h>
#import "Job.h"
Implement this Protocol @interface nurse:nsobject<Job>//
@end
Implementation file:
#import "Nurse.h"
@implementation Nurse
-(void) takeeat
{
NSLog (@ "The child is hungry, feed it to eat");
}
-(void) takesleep
{
NSLog (@ "The child is sleepy, coax him to sleep");
}
-(void) Takeplay
{
NSLog (@ "The child wakes up and plays with him");
}
-(void) Takeshower
{
NSLog (@ "Bathe children at Night");
}
-(void) dealloc
{
NSLog (@ "Nurse is Dealloc");
}
@end
Then declare a Morther class:
#import <Foundation/Foundation.h>
#import "Job.h"
@class nurse;
@interface Morther:nsobject
{
NSString *name;
Id<job> delegate; Declare an agent here so that mother can get the agent to do what it needs to do
}
-(ID) Initwithname: (NSString *) _name Delagat:(id<job>) _delagete; Incoming Agent
@property (nonatomic,copy) NSString *name;
-(void) delagatethings;//to be represented
@end
Implementation file
#import "Morther.h"
#import "Nurse.h"
@implementation Morther
-(ID) Initwithname: (NSString *) _name Delagat: (ID) _delagete
{
Self=[super Init];
if (self) {
if (name!=_name) {
[Name release];
Name=[_name copy];
[Delegate release];
Delegate=[_delagete retain];
}
}
return self;
}
@synthesize name;
-(void) dealloc
{
[Name release];
[Delegate release];
NSLog (@ "host is Dealloc");
}
-(void) delagatethings
{
int i;
switch (i) {
Case 1:
[Delegate Takeeat];
Break
Case 2:
[Delegate Takeplay];
Break
Case 3:
[Delegate Takeshower];
Break
Case 4:
[Delegate Takesleep];
Break
Default
Break
}
i++;
}
@end
See how it's implemented in the main file:
#import <Foundation/Foundation.h>
#import "Morther.h"
#import "Nurse.h"
#import "Job.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSString *name=[[nsstring alloc] initwithformat:@ "Floret"];
Nurse *fengjie=[[nurse alloc] init];//agent Fengjie
Morther *morther=[[morther alloc] Initwithname:name delagat:fengjie];//so that the delegate is passed into the mother object, So the Morther object can be done by nurse to do what she can't sit on.
[Name release];
[Nstimer scheduledtimerwithtimeinterval:2 Target:mortherselector: @selector (delagatethings) UserInfo:nil repeats: YES]; Call mechanism, 2 second call, call the object is Morther, call the method is delagatethings;
[[Nsrunloop Currentrunloop] run]; Keep the program running and make sure the call above can be done all the time.
[Morther release];
[Fengjie release];
}
return 0;
}
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 agent's method in B to complete the B-agent thing, that is, the thing that you delegate to B.
Objective-c proxy mode (delegate)