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; }