NSProxy
is an abstract superclass defining an API for objects that act as stand-ins for other objects or for objects that don’t exist yet. Typically, a message to a proxy is forwarded to the real object or causes the proxy to load (or transform itself into) the real object. Subclasses of NSProxy can be used to implement transparent distributed messaging (for example, NSDistantObject) or for lazy instantiation of objects that are expensive to create.
NSProxy 是一个虚基类,它为一些表现的像是其它对象替身或者并不存在的对象定义一套API。一般的,发送给代理的消息被转发给一个真实的对象或者代理本身load(或者将本身转换成)一个真实的对象。NSProxy的基类可以被用来透明的转发消息或者耗费巨大的对象的lazy 初始化。
NSProxy implements the basic methods required of a root class, including those defined in the NSObject protocol. However, as an abstract class it doesn’t provide an initialization method, and it raises an exception upon receiving any message it doesn’t respond to. A concrete subclass must therefore provide an initialization or creation method and overridethe forwardInvocation: and methodSignatureForSelect
or: methods to handle messages that it doesn’t implement itself. A subclass’s implementation of forwardInvocation: should do whatever is needed to process the invocation, such as forwarding the invocation over the network or loading the real object and passing it the invocation. methodSignatureForSelect
or: is required to provide argument type information for a given message; a subclass’simplementation should be able to determine the argument types for the messages it needs to forward and should construct an NSMethodSignature object accordingly. See the NSDistantObject, NSInvocation, and NSMethodSignature class specifications for more information.
NSProxy实现了包括NSObject协议在内基类所需的基础方法,但是作为一个虚拟的基类并没有提供初始化的方法。它接收到任何自己没有定义的方法他都会产生一个异常,所以一个实际的子类必须提供一个初始化方法或者创建方法,并且重载forwardInvocation:方法和methodSignatureForSelect
or:方法来处理自己没有实现的消息。一个子类的forwardInvocation:实现应该采取所有措施来处理invocation,比如转发网络消息,或者加载一个真实的对象,并把invocation转发给他。methodSignatureForSelect
or:需要为给定消息提供参数类型信息,子类的实现应该有能力决定他应该转发消息的参数类型,并构造相对应的NSMethodSignature对象。详细信息可以查看NSDistantObject, NSInvocation, and NSMethodSignature的类型说明。
Objective-C不支持多重继承,但是我们可以使用NSProxy的消息转发机制,来转发可由其它类的对象处理的任务,达成同样的目的。
Xcode的Documentation中有示例(搜索ForwardInvocation,在Sample Code类别中),如下:
/* |
File: main.m |
Abstract: This example shows how to do Objective C message forwarding in Foundation. |
Version: 1.0 |
|
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple |
Inc. ("Apple") in consideration of your agreement to the following |
terms, and your use, installation, modification or redistribution of |
this Apple software constitutes acceptance of these terms. If you do |
not agree with these terms, please do not use, install, modify or |
redistribute this Apple software. |
|
|
|
Copyright (C) 2009 Apple Inc. All Rights Reserved. |
|
*/ |
|
#import <Foundation/Foundation.h> |
#include <stdio.h> |
|
|
@interface TargetProxy : NSProxy { |
id realObject1; |
id realObject2; |
} |
|
- (id)initWithTarget1:(id)t1 target2:(id)t2; |
|
@end |
|
int main(int argc, const char *argv[]) { |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
// Create an empty mutable string, which will be one of the |
// real objects for the proxy. |
NSMutableString *string = [[NSMutableString alloc] init]; |
|
// Create an empty mutable array, which will be the other |
// real object for the proxy. |
NSMutableArray *array = [[NSMutableArray alloc] init]; |
|
// Create a proxy to wrap the real objects. This is rather |
// artificial for the purposes of this example -- you'd rarely |
// have a single proxy covering two objects. But it is possible. |
id proxy = [[TargetProxy alloc] initWithTarget1:string target2:array]; |
|
// Note that we can't use appendFormat:, because vararg methods |
// cannot be forwarded! |
[proxy appendString:@"This "]; |
[proxy appendString:@"is "]; |
[proxy addObject:string]; |
[proxy appendString:@"a "]; |
[proxy appendString:@"test!"]; |
|
NSLog(@"count should be 1, it is: %d", [proxy count]); |
|
if ([[proxy objectAtIndex:0] isEqualToString:@"This is a test!"]) { |
NSLog(@"Appending successful.", proxy); |
} else { |
NSLog(@"Appending failed, got: '%@'", proxy); |
} |
|
NSLog(@"Example finished without errors."); |
[pool release]; |
return 0; |
} |
|
|
@implementation TargetProxy |
|
- (id)initWithTarget1:(id)t1 target2:(id)t2 { |
realObject1 = [t1 retain]; |
realObject2 = [t2 retain]; |
return self; |
} |
|
- (void)dealloc { |
[realObject1 release]; |
[realObject2 release]; |
[super dealloc]; |
} |
|
// The compiler knows the types at the call site but unfortunately doesn't |
// leave them around for us to use, so we must poke around and find the types |
// so that the invocation can be initialized from the stack frame. |
|
// Here, we ask the two real objects, realObject1 first, for their method |
// signatures, since we'll be forwarding the message to one or the other |
// of them in -forwardInvocation:. If realObject1 returns a non-nil |
// method signature, we use that, so in effect it has priority. |
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { |
NSMethodSignature *sig; |
sig = [realObject1 methodSignatureForSelector:aSelector]; |
if (sig) return sig; |
sig = [realObject2 methodSignatureForSelector:aSelector]; |
return sig; |
} |
|
// Invoke the invocation on whichever real object had a signature for it. |
- (void)forwardInvocation:(NSInvocation *)invocation { |
id target = [realObject1 methodSignatureForSelector:[invocation selector]] ? realObject1 : realObject2; |
[invocation invokeWithTarget:target]; |
} |
|
// Override some of NSProxy's implementations to forward them... |
- (BOOL)respondsToSelector:(SEL)aSelector { |
if ([realObject1 respondsToSelector:aSelector]) return YES; |
if ([realObject2 respondsToSelector:aSelector]) return YES; |
return NO; |
} |
|
@end |
URL: