Ios_ Proxy Design Mode demo

Source: Internet
Author: User

Main.m

  main.m//  Proxy design mode/*    proxy design mode 3 use occasions        1,a want B to help him do something,    can let B become a agent first        2,a want to notify B,a something happened, Can let B become a agent first        3,b want to listen to a something happened,  can let B become a agent first    proxy set mode standard 4 step    1, define a protocol protocol    2,b want to do agent, The above agreement must be adhered to and implemented    3,a inside defines a member ID <protocol> delegate    4, assigns the instance object of B to the member variable of a delegate *///  Created by Beyond on 14-7-25.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <Foundation/Foundation.h> #import "Baby.h" #import "nana.h" int main (int argc, const char * argv[]) {    @autoreleasepool {                Baby *bb = [[Baby alloc]init];        Bb.age = 1;        Bb. Nanny = [[Nana Alloc]init];        The baby is hungry        [BB hungry];            }    return 0;}



Agreement

BabyNannyDelegate.h

  babynannydelegate.h//  Proxy setup mode////  Created by Beyond on 14-7-25.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <Foundation/Foundation.h> @class Baby; @protocol babynannydelegate <nsobject >//Baby Sitter Protocol Three methods//Feeding the baby-(void) Feedbaby: (Baby *) baby;//amused baby-(void) Amusebaby: (Baby *) baby;//slapping the baby, making it fall asleep-(void) Patbaby: (Baby *) baby; @end



Baby.h

  baby.h//  Proxy setup mode////  Created by Beyond on 14-7-25.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <Foundation/Foundation.h> #import "BabyNannyDelegate.h" @interface baby:nsobject/ /object is the strong ID on behalf of any object can be used as a babysitter, as long as the nanny agreement is adhered to and implemented in the Protocol of three methods @property (Nonatomic,strong) id<babynannydelegate> nanny;@ Property (nonatomic, assign) int age;-(void) cry;    Baby Crying-(void) sleep;  The baby wants to sleep-(void) hungry; The baby wants to breastfeed @end



Baby.m

  baby.m//  Proxy setup mode////  Created by Beyond on 14-7-25.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import "Baby.h" @implementation baby-(void) cry{    NSLog (@ "%d-year-old baby ... Cry ", _age);    Invoke the agent inside the method of amusing the baby    [_nanny amusebaby:self];} -(void) sleep{    NSLog (@ "%d-year-old baby ... Want to Sleep ", _age);    Call the agent inside the method of beating the baby to make it sleep    [_nanny patbaby:self];} -(void) hungry{    NSLog (@ "%d-year-old baby ... Want to Breastfeed ", _age);    Call the agent inside the method of feeding the baby    [_nanny feedbaby:self];} @end




Babysitter Nana.h

  nana.h//  Proxy setup mode////  Created by Beyond on 14-7-25.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <Foundation/Foundation.h> #import "BabyNannyDelegate.h"//Nana now wants to look after the baby, must abide by the baby-sitter agreement, and implement the Inside method @interface nana:nsobject<babynannydelegate> @end



Babysitter NANA.M

  nana.m//  Proxy setup mode////  Created by Beyond on 14-7-25.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import "Nana.h" #import "Baby.h" @implementation nana-(void) Feedbaby: (Baby *) baby{    NSLog (@ " Nana is feeding the baby at%d, baby.age); -(void) Amusebaby: (Baby *) baby{    NSLog (@ "Nana is coaxing%d-year-olds happy", baby.age);} -(void) Patbaby: (Baby *) baby{    NSLog (@ "Nana is gently patting the child of%d years of sleep", baby.age);} @end













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.