iOS_代理設計模式demo

來源:互聯網
上載者:User

iOS_代理設計模式demo

main.m

 

////  main.m//  代理設計模式/*    代理設計模式的3種使用場合        1,A 想讓B 幫他做些事情,    可以讓B成為A的代理先        2,A 想通知B,A發生了一些事情,可以讓B成為A的代理先        3,B 想監聽A發生了一些事情,  可以讓B成為A的代理先    代理設定模式的標準4步    1,定義一份protocol協議    2,B想做代理,必須先遵守並實現上面那份協議    3,A裡面定義一個成員 id  delegate    4,將B的執行個體對象,賦值給A的成員變數delegate *///  Created by beyond on 14-7-25.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import #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];        // 嬰兒餓了        [bb hungry];            }    return 0;}


 

 

協議

BabyNannyDelegate.h

 

////  BabyNannyDelegate.h//  代理設定模式////  Created by beyond on 14-7-25.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import @class Baby;@protocol BabyNannyDelegate // 嬰兒保姆協議中 規定三個方法// 給嬰兒餵奶- (void)feedBaby:(Baby *)baby;// 逗樂嬰兒- (void)amuseBaby:(Baby *)baby;// 拍打嬰兒,使其入睡- (void)patBaby:(Baby *)baby;@end


 

 

 

Baby.h

////  Baby.h//  代理設定模式////  Created by beyond on 14-7-25.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import #import "BabyNannyDelegate.h"@interface Baby : NSObject// 對象是strong id代表任何對象都可以作保姆,只要遵守保姆協議,並實現協議中的三個方法@property (nonatomic,strong) id Nanny;@property (nonatomic , assign) int age;- (void)cry;    // 嬰兒哭啦- (void)sleep;  // 嬰兒想睡啦- (void)hungry; // 嬰兒想吃奶了@end


 

 

Baby.m

 

////  Baby.m//  代理設定模式////  Created by beyond on 14-7-25.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import "Baby.h"@implementation Baby- (void)cry{    NSLog(@"%d歲的嬰兒...哭啦",_age);    // 調用代理裡面的逗樂嬰兒的方法    [_Nanny amuseBaby:self];}- (void)sleep{    NSLog(@"%d歲的嬰兒...想睡覺啦",_age);    // 調用代理裡面的拍打嬰兒使其入睡的方法    [_Nanny patBaby:self];}- (void)hungry{    NSLog(@"%d歲的嬰兒...想吃奶啦",_age);    // 調用代理裡面的給嬰兒餵奶的方法    [_Nanny feedBaby:self];}@end


 

 

 

保姆nana.h

 

////  nana.h//  代理設定模式////  Created by beyond on 14-7-25.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import #import "BabyNannyDelegate.h"// 娜娜 現在想照看嬰兒,必須遵守嬰兒保姆協議,並實現裡面的方法@interface nana : NSObject@end


 

 

 

保姆nana.m

////  nana.m//  代理設定模式////  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(@"娜娜正在給%d歲的嬰兒餵奶",baby.age);}- (void)amuseBaby:(Baby *)baby{    NSLog(@"娜娜正在哄%d歲的嬰兒開心",baby.age);}- (void)patBaby:(Baby *)baby{    NSLog(@"娜娜正在輕輕拍打%d歲的嬰兒入睡",baby.age);}@end


 

 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.