一、代理设计模式实例(中介找房)
1、需求分析
假设有一个叫jack的人(person),他想租一个公寓(Apartment),由于他工作繁忙(或者其他原因),没有时间去租房。因此,委托中介公司(Agent)帮他寻找房源,如果中介公司找到合适的房源就告知jack。
2、实例中知识点介绍
1)定时器的基本概念
一旦创建了一个定时器对象(NSTimer实例),它可以按照一定的时间间隔,将指定消息发送到目标对象,并更新某个对象的行为,你可以选择未来某个时候将它"开启",或者将它停止乃至销毁。
2)NSRunloop基本概念
一个Runloop就是一个事件处理的循环,用来不停调度工作以及处理输入事件。使用runloop的目的是让你的线程在有工作时忙于工作,而没工作的时候处于休眠状态。
在我们的应用程序中,你不需要创建NSRunloop对象。因为,在我们的主线程中(包含其他子线程),系统会自动创建NSRunloop对象。如果你需要访问当前线程中的Runloop,你可以通过"currentRunloop"访问。
3、实例相关代码
1)协议
// // FindApartment.h // DelegateDemo // // Created by moki on 14-8-5. // Copyright (c) 2014年 Santai. All rights reserved. // #import <Foundation/Foundation.h> typedef enum { HighRent = 0, MiddleRent = 1, LowRent = 2 } HourseRent; @class Person; @protocol FindApartment <NSObject> - (HourseRent)findApartment:(Person *)person; @end
2)代理类
// // Agent.h // DelegateDemo // // Created by moki on 14-8-5. // Copyright (c) 2014年 Santai. All rights reserved. // #import <Foundation/Foundation.h> #import "FindApartment.h" @interface Agent : NSObject <FindApartment> @end
// // Agent.m // DelegateDemo // // Created by moki on 14-8-5. // Copyright (c) 2014年 Santai. All rights reserved. // #import "Agent.h" #import "Person.h" @implementation Agent - (HourseRent)findApartment:(Person *)person { //NSLog(@"找到一所房子"); static int ghouserent = 1; HourseRent rent; if (ghouserent == 1) { NSLog(@"中介公司对%@说找到一个高价的公寓",person.name); rent = HighRent; } else if (ghouserent == 2) { NSLog(@"中介公司对%@说找到一个一般价位的公寓",person.name); rent = MiddleRent; } else{ NSLog(@"中介公司对%@说找到一个一低价位的公寓",person.name); rent = LowRent; } ghouserent++; return rent; } @end
3)person类
// // Person.h // DelegateDemo // // Created by moki on 14-8-5. // Copyright (c) 2014年 Santai. All rights reserved. // #import <Foundation/Foundation.h> #import "FindApartment.h" @interface Person : NSObject { @private NSString *_name; //注意,较高版本的xcode必须用__unsafe_unretained修饰,否则会编译报错 __unsafe_unretained id <FindApartment> _delegate; } @property (nonatomic,copy) NSString * name; @property (nonatomic,assign) id <FindApartment> delegate; - (id)initWithName:(NSString *)name withDelegate:(id <FindApartment>)delegate; - (void)wantToFindDeparment; @end
// // Person.m // DelegateDemo // // Created by moki on 14-8-5. // Copyright (c) 2014年 Santai. All rights reserved. // #import "Person.h" @interface Person () - (void)startFindApartment:(NSTimer *)timer; @end @implementation Person @synthesize name = _name; @synthesize delegate = _delegate; - (id)initWithName:(NSString *)name withDelegate:(id <FindApartment>)delegate { self = [super init]; if(self) { self.name=name; self.delegate=delegate; //设置代理 } return self; } - (void)wantToFindDeparment { [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(startFindApartment:) userInfo:nil repeats:YES]; } - (void)startFindApartment:(NSTimer *)timer { if ([self.delegate respondsToSelector:@selector(findApartment:)]) { HourseRent rent = [self.delegate findApartment:self]; if (rent == HighRent) { NSLog(@"%@说:房价太高,麻烦再找",self.name); } else if (rent == MiddleRent) { NSLog(@"%@说:房价一般,有没有再便宜的",self.name); } else{ NSLog(@"%@说:房价可以,谢谢。。。就这个了",self.name); [timer invalidate]; } printf("\n"); } } @end
5)main函数中调用
// // main.m // DelegateDemo // // Created by moki on 14-8-5. // Copyright (c) 2014年 Santai. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" #import "Agent.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); Agent *agent = [[Agent alloc] init]; Person *jack = [[Person alloc] initWithName:@"jack" withDelegate:agent]; [jack wantToFindDeparment]; //BOOL istrue = true; //while (istrue) { NSDate *date = [NSDate date]; [[NSRunLoop currentRunLoop] runUntilDate:[date dateByAddingTimeInterval:6]]; //istrue = false; NSLog(@"程序即将退出!"); //} //[[NSRunLoop currentRunLoop] run]; } return 0; }