@import url(http://www.shnenglu.com/cutesoft_client/cuteeditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
1 協(xié)議:
協(xié)議,類似于Java或C#語言中的接口,它限制了實現(xiàn)類必須擁有哪些方法。
它是對對象行為的定義,也是對功能的規(guī)范。
示例:
1
2
3
4
5
6
7
8
9
|
// GoodChild.h
#import <Foundation/Foundation.h>
@protocol GoodChild <NSObject>
-(void)filialPiety;
@end
|
1
2
3
4
5
6
7
8
|
// Student.h
#import <Foundation/Foundation.h>
#import "GoodChild.h"
//注意實現(xiàn)協(xié)議的語法。
@interface Student : NSObject<GoodChild>
@end
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// Student.m
// protocol
//
// Created by sxt on 11-10-23.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "Student.h"
@implementation Student
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
-(void)filialPiety{
NSLog(@"孝敬父母。。");
}
@end
|
此例中定義了一個協(xié)議GoodChild,類Student實現(xiàn)了此協(xié)議,所以必須有filialPiety方法。
每個類雖只有一個父類,但可以實現(xiàn)多個協(xié)議,以逗號隔開便可。語法如下:
1
2
3
|
@interface Student : NSObject<協(xié)議1,協(xié)議2>
@end
|
2 委托:
委托是objC中使用非常頻繁的一種設(shè)計模式,它的實現(xiàn)與協(xié)議的使用是分不開的,讓我們看一個綜合示例:
小公司老板日常的工作是管理公司、教導(dǎo)新員工、發(fā)工資與接電話。
其中管理公司、教導(dǎo)新員工是老板要親為的。
而發(fā)工資與接電話老板希望招聘一個秘書來幫忙,于是對秘書的要求就是要略懂出納發(fā)工資,要能幫助領(lǐng)導(dǎo)接電話。 而這兩項要求便是協(xié)議,對類功能的限定。
1
2
3
4
5
6
7
8
9
10
11
|
// SecProtocol.h
#import <Foundation/Foundation.h>
@protocol SecProtocol <NSObject>
//發(fā)工資
-(void)payoff;
//接電話
-(void)tel;
@end
|
然后定義一個秘書類
1
2
3
4
5
6
7
|
// Sec.h
#import <Foundation/Foundation.h>
#import "SecProtocol.h"
@interface Sec : NSObject<SecProtocol>
@end
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// Sec.m
#import "Sec.h"
@implementation Sec
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
-(void)payoff{
NSLog(@"sec payoff");
}
-(void)tel{
NSLog(@"sec tel");
}
@end
|
緊接著是老板類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// Boss.h
#import <Foundation/Foundation.h>
#import "SecProtocol.h"
@interface Boss : NSObject
//此屬性用于指定秘書對象,此對象必須實現(xiàn)SecProtocol協(xié)議。
@property(nonatomic,retain) id<SecProtocol> detegate;
//管理
-(void)manage;
//教導(dǎo)新員工
-(void)teach;
@end
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
// Boss.m
#import "Boss.h"
@implementation Boss
@synthesize detegate=_detegate;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
-(void)manage{
NSLog(@"boss manage");
}
-(void)teach{
NSLog(@"boss teach");
}
-(void)payoff{
NSAutoreleasePool *p=[[NSAutoreleasePool alloc] init];
[_detegate payoff];
[p release];
}
-(void)tel{
NSAutoreleasePool *p=[[NSAutoreleasePool alloc] init];
[_detegate tel];
[p release];
}
@end
|
那么老板就具有這4個方法,當(dāng)調(diào)用前2個時是自己完成功能,而調(diào)用后2個時則轉(zhuǎn)為調(diào)用秘書的方法。
此時我們跟秘書對象就叫做代理對象,代理模式的名字由來于此。
最后調(diào)用測試下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
// main.m
// delegate
//
// Created by sxt on 11-10-23.
// Copyright 2011年 Jinlong Wei. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Boss.h"
#import "Sec.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//實例化老板對象
Boss *boss=[[[Boss alloc] init] autorelease];
//實例化秘書對象
Sec *sec=[[[Sec alloc] init] autorelease];
//設(shè)置老板的代理對象為秘書
boss.detegate=sec;
//調(diào)用4個方法。
[boss payoff];
[boss tel];
[boss manage];
[boss teach];
[pool drain];
return 0;
}
|