1.protocol
- @protocol myProtocol <NSObject>
- @required
- - (int)add:(int) a and:(int)b;
- @optional
- - (int)minus:(int)a and:(int)b;
- @end
- @interface text : NSObject<myProtocol>
- @end
- @implementation text
- - (int)add:(int)a and:(int)b{
- return a + b;
- }
- - (int)minus:(int)a and:(int)b{
- if (a >= b) {
- return a - b;
- }
- else
- return b - a;
- }
- @end
iOS中Protocol和Java语言中的interface很类似,如果一些类之间没有继承关系,但是又具备某些相同的行为,则可以使用Protocol来描述它们的关系。不同的类,可以遵守同一个Protocol,在不同的场景下注入不同的实例,实现不同的功能。Cocoa框架中大量采用了这种模式实现数据和UI的分离。例如UIView产生的所有事件,都是通过委托的方式交给Controller完成。根据约定,框架中后缀为Delegate的都是Protocol,例如UIApplicationDelegate,UIWebViewDelegate等。
2.delegate
- #import <UIKit/UIKit.h>
- @protocol queryDelegate <NSObject>
- - (NSString *)query;
- @end
- @interface Person1 : UIViewController<queryDelegate>
- @end
- - (NSString *)query{
- NSString * result = [NSString stringWithFormat:@"热火赢太阳:103比98"];
- return result;
- }
首先定义了协议与方法,代理类person1遵循这个协议,并且实现了委托方法。
- #import <UIKit/UIKit.h>
- #import "Person1.h"
- @interface Person2 : UIViewController<queryDelegate>
- @end
person2.m
- - (NSString *)query{
- NSString * data =[NSString stringWithFormat:@"詹姆斯的数据是37分9篮板3助攻" ];
- return data;
- }
代理类person2同样遵循某个协议,并且实现了委托方法。
- #import <UIKit/UIKit.h>
- #import "Person1.h"
- #import "Person2.h"
- @interface delegateViewController : UIViewController
- @property (weak,nonatomic) id<queryDelegate> delegate;//声明delegate
- - (IBAction)button1Action:(id)sender;
- - (IBAction)button2Action:(id)sender;
- @property (weak, nonatomic) IBOutlet UILabel *labelResult;
- @property (weak, nonatomic) IBOutlet UILabel *labelData;
- @end
下面看delegateViewController.m:
- #import "delegateViewController.h"
- @interface delegateViewController ()
- @end
- @implementation delegateViewController
- @synthesize labelData;
- @synthesize labelResult;
- .....
- - (IBAction)button1Action:(id)sender {
- Person1 *pe1 = [[Person1 alloc] init];
- self.delegate = pe1;//设置代理对象为person1
- NSString * result = [self.delegate query];//通过委托变量调用委托方法
- [labelResult setText:result];
- }
- - (IBAction)button2Action:(id)sender {
- Person2 *pe2 = [[Person2 alloc]init];
- self.delegate = pe2;//设置代理对象为person2
- NSString* data = [self.delegate query] //通过委托变量调用委托方法
- [labelData setText:data];
- }
委托的主要价值是让你可以在一个核心对象里定制多个对象的功能特性。
2.构建helper object 的工具 Helper Object are commonly used to add functionality to an existing class without having to subclass it.
构建helper object,helper object用来对已有的类添加功能特性而不是通过子类继承实现。
比如ClassC
- @interface ClassC {
- id delegate;
- }
- @end
- @interface ClassC {
- ClassB *delegate;
- }
- @end
- @interface ClassC {
- ClassB *classB;
- }
- @end
.......按照上面这样写,delegate就没有了。
protocol是一种语法,它提供了一个很方便的、实现delegate模式的机会。
比如写UITableView的时候,Apple这么干
- - (void)doSomething {
- [self blahblah];
- [self.delegate guruguru];//放到delegate中去执行这个guruguru函数
- [self blahblah];
- }
- - (void)guruguru;
- @protocol XXXProtocol
- - (void)guruguru;
- @end
就说明了,UITableView需要的delegate是一个conform to XXXProtocol的类。这就正好是id<XXXProtocol>表达的意思。
另:非正式协议
显然这个名词是相对于正式协议而言的。在解释非正式协议之前,先引用两段话:
1、在《Cocoa设计模式》第六章类别的6.3.2把类别用于非正式协议一节中,这样写到:
非正式协议通常定义为NSObject的类别。类别接口中指定的方法可能会或者可能不会被框架类实际地实现。非正式协议位于一种设计灰区中。正式协议由编译器检查并且代表一种关于对象能力的保证,但是非正式协议不会做出保证----而只会给出提示。
2、苹果官方文档Cocoa Core Competencies一文中是这样介绍非正式协议的:
An informal protocol is a category on NSObject
, which implicitly makes almost all objects adopters of the protocol. (A category is a language feature that enables you to add methods to a class without subclassing it.) Implementation of the methods in an informal protocol is optional. Before invoking a method, the calling object checks to see whether the target object implements it. Until optional protocol methods were introduced in Objective-C 2.0, informal protocols were essential to the way Foundation and AppKit classes implemented delegation.
大概意思:非正式协议是NSObject类(显而易见,还包括它的子类)的类别,其所有的子类都含蓄地接受了这个协议。(类别是Objective-C的一个语言特点,可以让你在无需子类化的前提下为一个类增加方法。)非正式协议中的方法是否实现都是可选的,因此在调用非正式协议中的方法之前,需要去检查对象类是否实现了它。在Objective-C2.0中引入可选的正式协议方法之前,非正式协议是Foundation和AppKit类实现委托的唯一方式。
3、到底什么是非正式协议
综合1、2两段引文可以看出,所谓的非正式协议就是类别,即凡是NSObject或其子类的类别,都是非正式协议。