Ios-delegate design mode
In iOS proxy design mode, it can be decomposed into: host delegate actions Proctocol
Host: The role of this instance is a consumer, and it consumes the functionality provided by Proctocol
Delegate: is a laborer, it is mainly to provide proctocol services, it is quite to achieve the agreement
Action: A specific service implementation that is equivalent to delegate
The usage process for proxy mode is:
Host holds delegate references,
Delegate implementation of the Protocol, is the action of the specific implementation,
Action-specific services.
When did you want to spend it like this:
[Host.delegate action] This is called by the host to achieve the consumption of the agent.
Example:
The host class implements:
Header file:
Host desired service agreement @protocol hostdelegate <nsobject>-(void) sayhelloservice;-(void) saygoodbyeservice;@ End@interface Host:nsobject@property (nonatomic,assign) id
Implementation file:@implementation host-(void) customerhelloservice{ if ([Self.delegate respondstoselector: @selector ( Sayhelloservice)] { //Consumer Agent Services [self.delegate Sayhelloservice];} } -(void) customergoodbyeservice{ if ([Self.delegate respondstoselector: @selector (saygoodbyeservice)]) { // Services for consumer agents [Self.delegate saygoodbyeservice];} } @end
Implementation of proxy files
Agent header file:
The implementation of the Agreement, equivalent to the provision of services @interface delegateagent:nsobject
Proxy implementation files:
Specific services provided by the @implementation delegateagent//-(void) saygoodbyeservice{ NSLog (@ "Good bye Host");} Specific services provided-(void) sayhelloservice{ NSLog (@ "Hello host");} @end
Federated use of the specific proxy mode:
Create a proxy service instance delegateagent *dlgte =[[delegateagent alloc]init]; Create a consumer host *host = [[Host alloc]init]; Consumers to this service point consumption host.delegate = Dlgte; Consumers consume the following services [host Customergoodbyeservice]; [Host Customerhelloservice];
The specific service provided by the agent is the result of the output:
Agent-Output Services:
2015-03-29 17:45:50.890 target_action[2082:102628] Good bye host
2015-03-29 17:45:50.891 target_action[2082:102628] Hello Host
Run memory state for agent mode:
Ios-delegate design mode