delegate设计模式

使用场景

  • 当某些任务我们自己不想去实现,想让别人去实现的时候,
    就可以指定一个代理,让代理帮我们去做。我们只需要通知代理何时去做这些事情即可。

delegate的三个要素

  • delegate设计模式的使用我们要先需要明确三个要素-------委托方,代理方,协议
    • 委托方:委托别人去执行某些操作的 (对象)。
    • 协议(Protocol):委托 需要代理方执行的操作。
    • 代理方:被委托去执行某些操作的 (对象)。

delegate模式的设计步骤

  • 步骤1:委托方制定一套协议(在委托方的.h 件中),协议中声明委托方需要让代理方执行的方法(只有方法声明)。
  • 步骤2:委托方声明一个delegate属性(assign修饰),存储代理方对象。
  • 步骤3:代理方需要遵守协议,并且对协议中的方法进行实现。
  • 步骤4:将代理方设置为委托方的代理人(将代理方对象赋值给委托方对象的delegate属性,进行存储)。
    步骤5:委托方在合适的时机通知代理方对象去执行相应的操作。

案例

  • 一个单身汉自己带着不到一岁的儿子一起生活,单身汉不会做饭,洗衣服,打扫房间,也不会看孩子。想要找个保姆做上述事情.

  • 这个过程就是一个典型的delegate设计模式.
    单身汉是委托方,保姆就是代理方.协议就是干家务

  • 我们通过代码实现一下


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

@protocol doHousework 

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

- (void)lookafterBaby;

- (void)cleanHouse;

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

@end



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

@interface Person : NSObject

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


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


@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 

@protocol doHousework 

@required
- (void)washClothes;

- (void)lookafterBaby;

- (void)cleanHouse;

@optional

- (void)cook;

@end

@interface Person : NSObject

@property (nonatomic,assign)iddelegate;

- (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 
#import "Person.h"
@interface HouseKeeper : NSObject


- (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 
#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;
}

你可能感兴趣的:(delegate设计模式)