今天整理電腦翻到以前自學Objective-C時寫的一個練習委托設計模式的一個小程序,就po上來和大家分享,順便自己也復習一下OC中的委托。
Objective-C中的委托設計模式是和協議分不開的。
協議呢,就是使用了這個協議後就要按照這個協議來辦事,協議要求實現的方法就一定要實現。(在Objective-C2.0中可以在協議裡選擇是否必須實現某種方法,用關鍵字@optional和@required)
委托的話,顧名思義就是自己處理不了的事情,委托他人按照協議裡寫好的條款來辦理這件事。
具體實現的步驟可以按照下面這樣:
1.擬定一份協議,協議裡包括了想要實現的事情(即方法)。
2.委托人類中設置一個遵守該協議的委托變量。
3.被委托人類中實現協議要求的方法。
4.把委托人中的委托變量設置成被委托人。
5.當事情發生時,用委托變量調用被委托人中的協議方法。
下面講講我寫的這個例程,牛仔與姑娘。
故事背景是,在聯邦政府還沒有進入的荒野西部,一個善良美麗的姑娘Lucy和父親相依為命,過著艱苦但是還算幸福的生活。一天,一群土匪強盜的到來帶走了Lucy的父親,Lucy傷心至極,便委托好心的牛仔John Marston去解救他的父親,並將這群惡棍繩之以法。正義驅使John接下了這個活,並最終解救了Lucy的父親,並懲治了壞蛋。
好了,首先我們得先創建一份協議Wanted,包含兩個方法,saveHerDad和killTheBadGuy,代碼如下:
#import接著,創建姑娘Girl類,在其中設置一個遵守Wanted協議的委托變量,使用helpHelp方法來觸發事件,我用了定時器來模擬整個事件的發生,接口如下:@protocol Wanted @required -(int)saveHerDad; -(void)killTheBadGuy; @end
#import實現如下:#import "Wanted.h" @interface Girl : NSObject { } @property(nonatomic,copy) NSString *name; @property(nonatomic,assign) id delegate; -(id)initWithName:(NSString*)name WithDelegate:(id ) delegate; -(void)helpHelp; @end
#import "Girl.h" #import "Cowboy.h" @interface Girl () -(void)seesTheCowboy:(NSTimer *)timer; @end @implementation Girl -(id)initWithName:(NSString*)name WithDelegate:(id然後,創建牛仔Cowboy類,用他來實現協議Wanted中的方法,由於不想讓外部調用協議中的方法,所以我們將協議中的方法設置為私有方法,接口如下:) delegate{ self = [super init]; if (self) { self.name = name; self.delegate = delegate; } return self; } -(void)helpHelp{ NSLog(@"%@:My name is %@,my dad was taken by the bad guys,can you save him,please?",self.name,self.name); NSLog(@"%@:OK,young lady,wait for the good news!",self.delegate); [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(seesTheCowboy:) userInfo:@"My dad" repeats:YES]; } -(void)seesTheCowboy:(NSTimer *)timer{ if ([self.delegate respondsToSelector:@selector(saveHerDad)]) { int i = [self.delegate saveHerDad]; if (i==0) { NSLog(@"%@:My heart is so broken,I cant live without him,you must save him please,woo woo.",self.name); printf("\n"); }else{ NSLog(@"%@:Thank you so much,marry me~",self.name); [timer invalidate]; } } } @end
#import@interface Cowboy : NSObject @property (nonatomic,copy) NSString *name; @end
實現如下:
#import "Cowboy.h" @implementation Cowboy static int result; -(int)saveHerDad{ printf("\n"); result = [self killTheBadGuy]; if (result == 0) { NSLog(@"%@:There are too many of'em,I'll take all of my men and kill them all!!!Wait for the good news!",self.name); result++; return 0; }else{ NSLog(@"%@ and his crew:I killed all the bad guys,and here is your father.",self.name); return 1; } } -(int)killTheBadGuy{ return result; } -(NSString *)description { NSString *name = self.name; return name; } @end
好了,接下來我們就開始等著事件發生了,我設置了定時器,兩秒觸發一次,當result值為0的時候,營救並不成功,我們的牛仔铩羽而歸;當result值為1時,營救任務成功,解救了Lucy的父親。主函數登場:
#import#import "Girl.h" #import "Cowboy.h" int main(int argc, const char * argv[]) { @autoreleasepool { Cowboy *cowboy = [[Cowboy alloc] init]; //創建Cowboy實例 cowboy.name = @"John Marston"; Girl *girl = [[Girl alloc] initWithName:@"Lucy" WithDelegate:cowboy]; //初始化girl,將cowboy作為委托人 [girl helpHelp]; //觸發時間 NSDate *date = [NSDate date]; //設置定時器,程序共運行6秒 [[NSRunLoop currentRunLoop] runUntilDate:[date dateByAddingTimeInterval:6]]; } return 0; }
一切都准備就緒,現在讓我們的牛仔拿起槍,行動起來吧!
編譯成功!輸出結果如下:
2014-07-16 17:27:10.973 Girl&Cowboy[4055:303] Lucy:My name is Lucy,my dad was taken by the bad guys,can you save him,please?
2014-07-16 17:27:11.012 Girl&Cowboy[4055:303] John Marston:OK,young lady,wait for the good news!
2014-07-16 17:27:13.014 Girl&Cowboy[4055:303] John Marston:There are too many of'em,I'll take all of my men and kill them all!!!Wait for the good news!
2014-07-16 17:27:13.015 Girl&Cowboy[4055:303] Lucy:My heart is so broken,I cant live without him,you must save him please,woo woo.
2014-07-16 17:27:15.014 Girl&Cowboy[4055:303] John Marston and his crew:I killed all the bad guys,and here is your father.
2014-07-16 17:27:15.015 Girl&Cowboy[4055:303] Lucy:Thank you so much,marry me~
Program ended with exit code: 0
我們的英雄把姑娘的父親救出來了並且殺死了壞人!
這個例子很簡單,實際應用中會比這個復雜得多,但是希望通過我這個例子能讓不了解的同學對委托設計模式有個初步的認識吧~