@import url(http://www.shnenglu.com/cutesoft_client/cuteeditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
1 協(xié)議:
協(xié)議,類(lèi)似于Java或C#語(yǔ)言中的接口,它限制了實(shí)現(xiàn)類(lèi)必須擁有哪些方法。
它是對(duì)對(duì)象行為的定義,也是對(duì)功能的規(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"
//注意實(shí)現(xiàn)協(xié)議的語(yǔ)法。
@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
|
此例中定義了一個(gè)協(xié)議GoodChild,類(lèi)Student實(shí)現(xiàn)了此協(xié)議,所以必須有filialPiety方法。
每個(gè)類(lèi)雖只有一個(gè)父類(lèi),但可以實(shí)現(xiàn)多個(gè)協(xié)議,以逗號(hào)隔開(kāi)便可。語(yǔ)法如下:
1
2
3
|
@interface Student : NSObject<協(xié)議1,協(xié)議2>
@end
|
2 委托:
委托是objC中使用非常頻繁的一種設(shè)計(jì)模式,它的實(shí)現(xiàn)與協(xié)議的使用是分不開(kāi)的,讓我們看一個(gè)綜合示例:
小公司老板日常的工作是管理公司、教導(dǎo)新員工、發(fā)工資與接電話。
其中管理公司、教導(dǎo)新員工是老板要親為的。
而發(fā)工資與接電話老板希望招聘一個(gè)秘書(shū)來(lái)幫忙,于是對(duì)秘書(shū)的要求就是要略懂出納發(fā)工資,要能幫助領(lǐng)導(dǎo)接電話。 而這兩項(xiàng)要求便是協(xié)議,對(duì)類(lèi)功能的限定。
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
|
然后定義一個(gè)秘書(shū)類(lèi)
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
|
緊接著是老板類(lèi):
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
//此屬性用于指定秘書(shū)對(duì)象,此對(duì)象必須實(shí)現(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個(gè)方法,當(dāng)調(diào)用前2個(gè)時(shí)是自己完成功能,而調(diào)用后2個(gè)時(shí)則轉(zhuǎn)為調(diào)用秘書(shū)的方法。
此時(shí)我們跟秘書(shū)對(duì)象就叫做代理對(duì)象,代理模式的名字由來(lái)于此。
最后調(diào)用測(cè)試下:
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];
//實(shí)例化老板對(duì)象
Boss *boss=[[[Boss alloc] init] autorelease];
//實(shí)例化秘書(shū)對(duì)象
Sec *sec=[[[Sec alloc] init] autorelease];
//設(shè)置老板的代理對(duì)象為秘書(shū)
boss.detegate=sec;
//調(diào)用4個(gè)方法。
[boss payoff];
[boss tel];
[boss manage];
[boss teach];
[pool drain];
return 0;
}
|