iOS常用设计模式之委托模式
2015-07-19 18:21
615 查看
委托模式在Cocoa Touch框架和Cocoa框架中都有很多的应用。例如在应用启动的时候需要的一个类:UIApplication。在程序的入口函数main函数里面:
UIApplication的方法UIApplicationMain函数设置了它的委托类:WBAppDelegate。UIApplication不直接依赖于WBAppDelegate类,而是依赖于UIApplicationDelegate协议,WBAppDelegate类实现协议UIApplicationDelegate,WBAppDelegate是一个委托类。
委托是为了降低一个对象的复杂度和耦合度,使其能够更具通用性而将一些处理置于委托对象中得编码方式。通用类因为通用性而变成框架类,框架类保持委托对象的指针,并在特定的时刻向委托对象发送消息。消息可能只是通知委托对象做一些事情,也可能是对委托对象的控制。
下面通过一个班主任老师和班长的例子来介绍一下委托模式的实现原理和应用场景。老师(Teacher)为了更好的管理班级需要任命一位同学为班长(Monitor),老师希望班长能帮助他做两件事情:
1、发布一些信息(sendMessage)
2、收集同学们的一些情况(collectSomething)
也就是说,老师为了更好地工作,需要将这两件事情委托给班长来做,然而要成为班长,需要实现一个协议,这个协议要求能够处理“发布一些信息”和“收集同学们的一些情况”。先定义一个Teacher类:
Teacher类就是上面所说的通用类,它通过delegate属性保持委托对象的引用,委托对象就是班长,他需要实现协议WBTeacherDelegate。WBTeacherDelegate协议规定了两个方法:sendMessage和collectSomething。
下面来实现班长类:
然后修改一下Teacher类:
可以看到,委托协议WBTeacherDelegate定义了两个方法,他得实现类是WBMonitor。WBTeacher类中定义doSometing:方法是为了调试方便模拟老师向班长发送消息的方法。
委托对象和通用类通过WBMonitor类中的init方法中的teacher.delegate = self来建立关系。调试输出内容如下:
附WBTeacher和WBMonitor类的下载地址:委托模式
int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([WBAppDelegate class])); } }
UIApplication的方法UIApplicationMain函数设置了它的委托类:WBAppDelegate。UIApplication不直接依赖于WBAppDelegate类,而是依赖于UIApplicationDelegate协议,WBAppDelegate类实现协议UIApplicationDelegate,WBAppDelegate是一个委托类。
委托是为了降低一个对象的复杂度和耦合度,使其能够更具通用性而将一些处理置于委托对象中得编码方式。通用类因为通用性而变成框架类,框架类保持委托对象的指针,并在特定的时刻向委托对象发送消息。消息可能只是通知委托对象做一些事情,也可能是对委托对象的控制。
下面通过一个班主任老师和班长的例子来介绍一下委托模式的实现原理和应用场景。老师(Teacher)为了更好的管理班级需要任命一位同学为班长(Monitor),老师希望班长能帮助他做两件事情:
1、发布一些信息(sendMessage)
2、收集同学们的一些情况(collectSomething)
也就是说,老师为了更好地工作,需要将这两件事情委托给班长来做,然而要成为班长,需要实现一个协议,这个协议要求能够处理“发布一些信息”和“收集同学们的一些情况”。先定义一个Teacher类:
#import @protocol WBTeacherDelegate @required - (void)sendMessage; - (void)collectSomething; @end @interface WBTeacher : NSObject @property (nonatomic, weak) id delegate; @end
// // WBTeacher.m // DesignPatternsDemo // // Created by 韩学鹏 on 15/6/22. // Copyright (c) 2015年 韩学鹏. All rights reserved. // #import "WBTeacher.h" @implementation WBTeacher @end
Teacher类就是上面所说的通用类,它通过delegate属性保持委托对象的引用,委托对象就是班长,他需要实现协议WBTeacherDelegate。WBTeacherDelegate协议规定了两个方法:sendMessage和collectSomething。
下面来实现班长类:
// // WBMonitor.h // DesignPatternsDemo // // Created by 韩学鹏 on 15/6/22. // Copyright (c) 2015年 韩学鹏. All rights reserved. // #import #import "WBTeacher.h" @interface WBMonitor : NSObject @end
// // WBMonitor.m // DesignPatternsDemo // // Created by 韩学鹏 on 15/6/22. // Copyright (c) 2015年 韩学鹏. All rights reserved. // #import "WBMonitor.h" @implementation WBMonitor - (id)init { self = [super init]; WBTeacher *teacher = [[WBTeacher alloc] init]; teacher.delegate = self; [teacher doSomething:0]; [teacher doSomething:1]; return self; } - (void)sendMessage { NSLog(@"monitor:send message..."); } - (void)collectSomething { NSLog(@"monitor:collect something..."); } @end
然后修改一下Teacher类:
// // WBTeacher.h // DesignPatternsDemo // // Created by 韩学鹏 on 15/6/22. // Copyright (c) 2015年 韩学鹏. All rights reserved. // #import @protocol WBTeacherDelegate @required - (void)sendMessage; - (void)collectSomething; @end @interface WBTeacher : NSObject @property (nonatomic, weak) id delegate; - (void)doSomething:(int)tag; @end // // WBTeacher.m // DesignPatternsDemo // // Created by 韩学鹏 on 15/6/22. // Copyright (c) 2015年 韩学鹏. All rights reserved. // #import "WBTeacher.h" @implementation WBTeacher - (void)doSomething:(int)tag { if (!_delegate) { return; } switch (tag) { case 0: [_delegate sendMessage]; break; case 1: [_delegate collectSomething]; break; default: break; } } @end
可以看到,委托协议WBTeacherDelegate定义了两个方法,他得实现类是WBMonitor。WBTeacher类中定义doSometing:方法是为了调试方便模拟老师向班长发送消息的方法。
委托对象和通用类通过WBMonitor类中的init方法中的teacher.delegate = self来建立关系。调试输出内容如下:
2015-06-22 09:08:47.991 DesignPatternsDemo[26365:607] monitor:send message... 2015-06-22 09:08:47.993 DesignPatternsDemo[26365:607] monitor:collect something...
附WBTeacher和WBMonitor类的下载地址:委托模式
相关文章推荐
- iOS常用设计模式之单例模式
- IOS TableView滑动不灵敏问题
- IOS数组的排序和筛选
- IOS取消performSelector警告
- IOS艺术字及简单的图文混排
- IOS计算文字高度
- IOS之——快速获取苹果设备UDID(插曲)
- ios文章list
- ios文章list
- 28-手势 响应者链 手势识别器
- iOS设计模式-桥接
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解 .
- ios 线程同步
- IOS SEL (@selector) 原理及使用总结(二)
- IOS SEL (@selector) 原理及使用总结(一)
- IOS 解决CG截图功能出现图片翻转的情况
- iOS之新建工程上下出现黑色块
- (七十五)CoreLocation(一)在iOS7和iOS8设备上获取授权
- (七十五)CoreLocation(一)在iOS7和iOS8设备上获取授权
- 苹果开发者证书生成流程