iOS学习笔记

delegate设计模式

2016-08-08  本文已影响1035人  woooooo

使用场景

delegate的三个要素

delegate模式的设计步骤

案例


步骤1:
在person.h中声明协议doHousework

@protocol doHousework <NSObject>

@required
//@required属性下的方法是必须实现的
- (void)washClothes;

- (void)lookafterBaby;

- (void)cleanHouse;

@optional
//@optional属性下的方法可以选择实现
- (void)cook;

@end



步骤2:
在person.h中声明协议doHousework的delegate属性

@interface Person : NSObject

@property (nonatomic,assign)id<doHousework>delegate;
//声明 delegate属性存储代理人对象。<>代表遵循协议 
//为什么用assign修饰该属性
  assign 是直接赋值,而retain是引用引数加1;使用retain容易出现内存错误(如出现循环引用等问题)。
  一个对象没必要管理自己delegate的生命周期,或者说没必要拥有该对象,所以只要知道它的指针就可以了,用指针找到对象去调用方法,也就是委托实现的感觉。


@end
//第三步  保姆遵守协议  HouseKeeper.h文件中
#import <Foundation/Foundation.h>
#import "Person.h"
@interface HouseKeeper : NSObject<doHousework>


@end
步骤3:保姆实现协议方法

#import "HouseKeeper.h"

@implementation HouseKeeper


- (void)washClothes {
    NSLog(@"洗衣服");
}

- (void)lookafterBaby {
    NSLog(@"照顾孩子");
}
- (void)cleanHouse {
    NSLog(@"打扫房间");
}
//做饭的操作可以选择实现
- (void)cook {
    NSLog(@"做饭");
}
@end
步骤4:
将保姆设置为单身汉的代理人 在main.m文件中完成的操作

   Person *person = [[Person alloc] init];
    //创建单身汉对象
    HouseKeeper *houseKeeper = [[HouseKeeper alloc] init];
    //创建保姆对象
    person.delegate =houseKeeper;
    //将保姆对象设置为单身汉的代理人对象

步骤5:单身汉在适合的情况下通知保姆做相应的操作

//模拟饥饿的操作
if ([person.delegate respondsToSelector:@selector(cooking)]) {
//通知保姆去做饭
     [person.delegate cook];
}
//注:respondsToSelector:该方法判断对象是否能够对某一个选定的方法做出响应。

完整代码

Person.h

#import <Foundation/Foundation.h>

@protocol doHousework <NSObject>

@required
- (void)washClothes;

- (void)lookafterBaby;

- (void)cleanHouse;

@optional

- (void)cook;

@end

@interface Person : NSObject

@property (nonatomic,assign)id<doHousework>delegate;

- (void)clothesDirty;
- (void)hungry;
- (void)babyCry;
- (void)HouseDirty;
@end



Person.m

#import "Person.h"

@implementation Person

- (void)clothesDirty {
    NSLog(@"衣服脏了");
    //让自己的代理人去执行washClothes方法
    [self.delegate washClothes];
    
}
- (void)HouseDirty {
    NSLog(@"房子脏了");
    [self.delegate cleanHouse];
}

- (void)babyCry {
    NSLog(@"宝宝哭了");
    
    [self.delegate lookafterBaby];
}

- (void)hungry {
    NSLog(@"饿了");
    if ([self.delegate respondsToSelector:@selector(cook)]) {
    //如果保姆相应了cook方法
         [self.delegate cook];
    }
    else {
        NSLog(@"自己做");
    }
   
    
    
}
@end


HouseKeeper.h

#import <Foundation/Foundation.h>
#import "Person.h"
@interface HouseKeeper : NSObject<doHousework>


- (void)washClothes;
- (void)cook;
- (void)lookafterBaby;
- (void)cleanHouse;

@end


HouseKeeper.m


#import "HouseKeeper.h"

@implementation HouseKeeper


- (void)washClothes {
    NSLog(@"洗衣服");
}
- (void)cook {
    NSLog(@"做饭");
}
- (void)lookafterBaby {
    NSLog(@"照顾孩子");
}
- (void)cleanHouse {
    NSLog(@"打扫房间");
}

@end


main.m

#import <Foundation/Foundation.h>
#import "Person.h"
#import "HouseKeeper.h"
int main(int argc, const char * argv[]) {

    Person *person = [[Person alloc] init];
    //创建单身汉对象
    HouseKeeper *houseKeeper = [[HouseKeeper alloc] init];
    //创建保姆对象
    
    person.delegate =houseKeeper;
    //将保姆设置为单身汉的代理人对象
    [person hungry];
    [person babyCry];
    [person HouseDirty];
    [person clothesDirty];
    
    
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读