1、代理设计模式实例(中介找房)
设计模式
一、需求分析
xcode
假设有一个叫jack的人(person),他想租一个公寓(Apartment),因为他工做繁忙(或者其余缘由),没有时间去租房。所以,委托中介公司(Agent)帮他寻找房源,若是中介公司找到合适的房源就告知jack。函数
二、实例中知识点介绍
oop
1)定时器的基本概念
atom
一旦建立了一个定时器对象(NSTimer实例),它能够按照必定的时间间隔,将指定消息发送到目标对象,并更新某个对象的行为,你能够选择将来某个时候将它"开启",或者将它中止乃至销毁。
spa
2)NSRunloop基本概念
线程
一个Runloop就是一个事件处理的循环,用来不停调度工做以及处理输入事件。使用runloop的目的是让你的线程在有工做时忙于工做,而没工做的时候处于休眠状态。
设计
在咱们的应用程序中,你不须要建立NSRunloop对象。由于,在咱们的主线程中(包含其余子线程),系统会自动建立NSRunloop对象。若是你须要访问当前线程中的Runloop,你能够经过"currentRunloop"访问。
代理
三、实例相关代码
code
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; }