老板太忙了,所以雇用了一位助手,在合同(协议)中安排了一些任务A,任务B,任务C,..给助手完成;老板只管接活,不管具体怎么实现,只要知道结果,具体怎么完成由助手去解决;那么,“老板”就是A Object.,和"老板"签合同(Delegate)的是助手,写成代码就是:老板.delegate=助手;
助手是可以实现老板给他的任务A,B,C,..的,这些任务就是合同(协议 protocol)中申明的任务,由助手来实现合同(协议)所声明的任务。
协议 Protocol :(类似java的接口, C++的虚基类)
object-c 里没有多继承。那么又要避免做出一个对象什么都会(superclass monster,huge ,super,waste)一个超能对象本身是否定了面向对象的概念和真谛了。为了让代码更简洁,条理更清楚,可以将部分职责分离。
/*****************************************************************/
/************************* 协议 **************************//*****************************************************************/
//定义一个协议;协议本身没有具体的实现。只规定了一些可以被其它类实现的接口;
//@required 是协议里用来定义必须实现的方法 (默认的就是@required,还有个是可选的,叫@optional)
//
// MyProtocol.h
// Created by Test on 12-11-20.
// Copyright 2012 Test. All rights reserved.
#import <Foundation/Foundation.h>
@protocol MyProtocol <NSObject>
@required
-(void)testProFun;
@optional
-(void)testProFun2;
@end
/*****************************************************************/
/************************** 老板 **************************/
/*****************************************************************/
//需要调用代理的类的申明;
//
// MyProtocolRef.h
// Created by Test on 12-11-20.
// Copyright 2012 Test. All rights reserved.
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@interface MyProtocolRef : NSObject
{
// 申明一个代理类的实例;
id<MyProtocol> _delegate;
}
//delegate 总是被定义为 assign @property
@property(nonatomic,assign)id<MyProtocol>delegate;
@end
//需要调用代理的类的实现;
//在MyProtocolRef 内部声明一个委托(delegate),那么就需要委托的代理实现MyProtocol中约定的行为;
//
// MyProtocolRef.m
// Created by Test on 12-11-20.
// Copyright 2012 Test. All rights reserved.
#import "MyProtocolRef.h"
@implementation MyProtocolRef
@synthesize delegate=_delegate;
//初始化;
-(id)init
{
self =[super init];
if (self) {
}
return self;
}
//调用代理的方法;(老板接活)
-(void)callDelegate
{
if ([self.delegate respondsToSelector:@selector(testProFun)]) {
[self.delegate testProFun];
}
if ([self.delegate respondsToSelector:@selector(testProFun2)]) {
[self.delegate testProFun2];
}
}
@end
/*****************************************************************/
/************************* 助手 **************************/
/*****************************************************************/
// 首先, 在接口里边声明要使用谁的Delegate
//需要调用代理的类的实现;
//
// MyProtocolImp.h
// Created by Test on 12-11-20.
// Copyright 2012 Test. All rights reserved.
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@class MyProtocolRef;
//申明使用MyProtocol的Delegate;
@interface MyProtocolImp : NSObject<MyProtocol>
{
MyProtocolRef *_ref;
}
@end
// 然后在实现文件中初始化的时候, 设置Delegate为self(自己) ,
//并实现MyProtocol中约定的行为
//
// MyProtocolImp.m
// Created by Test on 12-11-20.
// Copyright 2012 Test. All rights reserved.
#import "MyProtocolImp.h"
#import "MyProtocolRef.h"
@implementation MyProtocolImp
-(id)init
{
self=[super init];
if (self) {
_ref=[[MyProtocolRef alloc]init];
// 设置Delegate为self(自己);
_ref.delegate=self;
}
return self;
}
-(void)dealloc
{
[_ref release];
_ref=nil;
[super dealloc];
}
-(void)callRef
{
if (_ref) {
[_ref callDelegate];
}
}
//imp MyProtocol testProFun
-(void)testProFun
{
NSLog(@"call testProFun");
}
//imp MyProtocol testProFun2
-(void)testProFun2
{
NSLog(@"call testProFun2");
}
@end