设计模式:代理
2015-12-03 17:06
120 查看
1.通过中介公司找房子
View Code
//Person.h #import <Foundation/Foundation.h> @class Person; @protocol PersonProtocol <NSObject> -(void)personWantToFindApartment:(Person *)person; @end //Person要找房子 @interface Person : NSObject @property (nonatomic, assign) NSString *name; //找房子的代理 @property (nonatomic, strong) id<PersonProtocol>delegate; //构造方法 -(instancetype)initWithName:(NSString *)name withDelegate:(id<PersonProtocol>)delegate; //找房子 -(void)wantToFindApartment; @end //Person.m #import "Person.h" //定义私有方法 @interface Person() -(void)startFindApartment:(NSTimer *)timer; @end //实现Person @implementation Person -(instancetype) initWithName:(NSString *)name withDelegate:(id<PersonProtocol>)delegate{ self = [super init]; if (self) { self.name = name; self.delegate = delegate; } return self; } -(void) wantToFindApartment{ [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(startFindApartment:) userInfo:@"代理找房子" repeats:YES]; } //实现私有方法 -(void) startFindApartment:(NSTimer *)timer{ NSString *info = [timer userInfo]; NSLog(@"1"); if ([self.delegate respondsToSelector:@selector(personWantToFindApartment:)]) { NSLog(@"%@",info); [self.delegate personWantToFindApartment:self]; } } @end //Agent.h //代理 #import <Foundation/Foundation.h> @protocol PersonProtocol; @interface Agent : NSObject<PersonProtocol> @end //Agent.m #import "Agent.h" #import "Person.h" @implementation Agent -(void)personWantToFindApartment:(Person *)person{ NSLog(@"%s 代理帮忙找房子",__func__); } @end //main.m #import <Foundation/Foundation.h> #import "Person.h" #import "Agent.h" int main(int argc, const char * argv[]) { @autoreleasepool { Agent *a = [[Agent alloc] init]; Person *p = [[Person alloc] initWithName:@"keen" withDelegate:a]; [p wantToFindApartment]; [[NSRunLoop currentRunLoop] run]; } return 0; }
View Code
相关文章推荐
- SQL注入攻击
- Solr部署到Tomcat
- [置顶] log4net 配置文件相对路径配置
- 自我修养——目标文件.o有什么
- struts2中的constant配置详解
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- 七、Base64
- Linux学习笔记--4.Linux 目录结构及文件基本操作
- Linux学习笔记--4.Linux 目录结构及文件基本操作