Agent protocol for communication between iOS development ———— objects

Source: Internet
Author: User

First, the agent to achieve inter-object communication:

Determine who is the proxy object and who is the proxy object.

Where did the event occur? Who is the proxy object

Where does the event respond? Who is the proxy object

Demand:

Example: The babysitter takes care of the child, cleans it when the child is dirty, and plays with the child when she is not happy.

by proxy class Child

defines the proxy attribute (ID), and this proxy property adheres to the Protocol

When an event occurs, the protocol method of the Proxy property is invoked to respond to this event.

proxy class Nanny

to abide by the agreement

Implementation of the Protocol method, the content of the Protocol method is to respond to the event.


The solution of proxy design pattern is the problem of program architecture, which makes the program architecture more reasonable, more extensible, and reduces the coupling between class and class.


Required objects:

proxy object:Nanny, Grandma

Object being proxied : Child

Process:

1. define a protocol in which some methods are declared.

2. define the proxy class compliance protocol, which contains the declaration of all methods of the protocol.

3. the proxy class should define a proxy object, set the type to the generic type id< protocol name > delegate;

4. When the Proxied object is about to complete a function that it cannot complete, it notifies the proxy object to help him complete, which is done by invoking the Protocol method implemented in the proxy class.

First set up the Protocol class

#import <Foundation/Foundation.h> @class child;//The agreement to care for a child must comply with this agreement in order to take care of the child's eligibility @protocol Carebabyprotocol < nsobject>//give the Child a bath-(void) Bathechild: (Child *) child;//fun-(void) Playwithchild: (Child *), @end

Defining proxy objects in a child class

#import "CareBabyProtocol.h" #import <Foundation/Foundation.h> @interface child:nsobject@property Nsinteger Cleanvalue; Cleanliness of @property Nsinteger happyvalue; Happiness//A proxy object should be defined in the proxy class, set to the generic type id< protocol name > delegate;//the child has a generic (ID) proxy object, and let this object comply with the protocol @property id< Carebabyprotocol> delegate;-(Instancetype) Initwithcleanvalue: (Nsinteger) Cleanvalue happyValue: (NSInteger) Happyvalue; @end

Implement the method in the agent when the child is not clean or unhappy

#import "Child.h" @implementation child-(Instancetype) Initwithcleanvalue: (Nsinteger) Cleanvalue Happyvalue: ( Nsinteger) Happyvalue {self        = [super init];        if (self) {                Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (timeraction :) Userinfo:nil Repeats:yes];                _cleanvalue = Cleanvalue;                _happyvalue = Happyvalue;                    }        return self;} -(void) Timeraction: (Nstimer *) timer {        _cleanvalue--;    _happyvalue--;        NSLog (@ "Clean:%li, Happy:%li", _cleanvalue, _happyvalue);        When the cleanliness of the child drops a certain limit, inform the babysitter to bathe him    if (_cleanvalue = =) {                [self.delegate bathechild:self];                    }        if (_happyvalue = =) {        [self.delegate playwithchild:self];    }    } @end

Define proxy object Nanny and let the babysitter sign the agreement

#import "CareBabyProtocol.h" #import <Foundation/Foundation.h> @class child; @interface Nanny:nsobject < Carebabyprotocol> @end

The implementation of the nanny's protocol method

#import "Nanny.h" #import "Child.h" @implementation nanny-(void) Bathechild: (Children *) Child {        NSLog (@ "The baby is dirty, the babysitter washes the child in vain.) ");    Child.cleanvalue = +;    } -(void) Playwithchild: (Children *) Child {        NSLog (@ "The kid is noisy, the nanny makes him happy");    Child.happyvalue = +;    } @end

Open child object and proxy object in main (nanny)

#import "Child.h" #import "Nanny.h" #import "Grandma.h" #import <foundation/foundation.h>int main (int argc, const char * argv[]) {    @autoreleasepool {                        //nanny *nanny = [[Nanny alloc] init];        Grandma *GRANDMA = [[Grandma alloc] init];                Child *child = [[Child alloc] initwithcleanvalue:100 happyvalue:100];                The nanny as the child's surrogate object        //child.nanny = Nanny;                The grandmother as the child's proxy object        child.delegate = grandma;                        [[Nsrunloop Currentrunloop] run];                            }    return 0;}



Agent protocol for communication between iOS development ———— objects

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.