简单的例子。。。说说自己对代理模式的理解。1.buttonL.h 定义一个buttonL类,这个类只有一个方法,通知保镖我被打了
- #import <Foundation/Foundation.h>
- #import "baobiaoxieyi.h"
- @interface buttonL : NSObject
- //定义一个被打的地方
- @property (nonatomic,copy)NSString *place;
- //保镖遵守保镖协议
- @property (nonatomic,strong)id<baobiaoxieyi> baobiao;
- //被打了的方法
- -(void)hited;
- @end
复制代码 2.buttonL.m
3baobiaoxieyi.h(协议)
- #import <Foundation/Foundation.h>
- @protocol baobiaoxieyi <NSObject>
- -(void)revenge:(NSString *)place;
- @end
复制代码 4baobiao.h
- #import <Foundation/Foundation.h>
- #import "baobiaoxieyi.h"
- @interface baobiao : NSObject<baobiaoxieyi>
- @end
复制代码 5.baobiao.m
- #import "baobiao.h"
- @implementation baobiao
- -(void)revenge:(NSString *)place{
- NSLog(@"保镖赶到%@,打走了暴徒",place);
- }
- @end
复制代码 6mian函数
- #import <Foundation/Foundation.h>
- #import "buttonL.h"
- #import "baobiao.h"
- int main(int argc, const char * argv[])
- { @autoreleasepool {
- buttonL *btn=[[buttonL alloc] init];
- baobiao *bao1=[[baobiao alloc] init];
- btn.place=@"黑马训练营";
- btn.baobiao=bao1;
- [btn hited]; }
- return 0;
- }
复制代码 7.输出结果
- 2014-09-27 14:16:50.693 简单的代理模式[1611:303] 我在黑马训练营被打了.
- 2014-09-27 14:16:50.695 简单的代理模式[1611:303] 保镖赶到黑马训练营,打走了暴徒
复制代码
|