Ios-delegate design mode

Source: Internet
Author: User


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

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.