代理模式的原理解析
代理模式(Proxy Design Pattern)在不改变原始类代码的情况下,通过引入代理类来给原始类附加功能。
@interface DMRequestInfo : NSObject
@property (nonatomic, strong) NSString *keyWord;
@property (nonatomic, assign) NSTimeInterval responseTime;
@property (nonatomic, assign) NSTimeInterval startTime;
@end
@implementation DMRequestInfo
@end
@interface DMMetricsCollector : NSObject
- (void)recordRequest:(DMRequestInfo *)requestInfo;
@end
@implementation DMMetricsCollector
- (void)recordRequest:(DMRequestInfo *)requestInfo
{
}
@end
@interface DMUserController : NSObject
- (instancetype)initWithMetricsCollector:(DMMetricsCollector *)metricsCollector;
- (void)userLogin:(NSString *)telephone password:(NSString *)password;
- (void)userRegister:(NSString *)telephone password:(NSString *)password;
@end
@interface DMUserController ()
@property (nonatomic, strong) DMMetricsCollector *metricsCollector;
@end
@implementation DMUserController
- (instancetype)initWithMetricsCollector:(DMMetricsCollector *)metricsCollector
{
if (self = [super init]) {
self.metricsCollector = metricsCollector;
}
return self;
}
- (void)userLogin:(NSString *)telephone password:(NSString *)password
{
NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
// 省略登录逻辑
NSTimeInterval endTime = [[NSDate date] timeIntervalSince1970];
NSTimeInterval responseTime = endTime - startTime;
DMRequestInfo *requestInfo = [[DMRequestInfo alloc] init];
requestInfo.keyWord = @"login";
requestInfo.responseTime = responseTime;
requestInfo.startTime = startTime;
[self.metricsCollector recordRequest:requestInfo];
}
- (void)userRegister:(NSString *)telephone password:(NSString *)password
{
NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
// 省略注册逻辑
NSTimeInterval endTime = [[NSDate date] timeIntervalSince1970];
NSTimeInterval responseTime = endTime - startTime;
DMRequestInfo *requestInfo = [[DMRequestInfo alloc] init];
requestInfo.keyWord = @"register";
requestInfo.responseTime = responseTime;
requestInfo.startTime = startTime;
[self.metricsCollector recordRequest:requestInfo];
}
@end
上面的写法有两个问题:
- 1.性能计数器框架代码侵入到业务代码中,跟业务代码高度耦合。如果未来需要替换这个框架,那替换成本会比较大。
- 2.收集接口请求的代码跟业务代码无关,本就不应该放到一个类中。业务类最好职责更加单一,只聚焦业务处理。
为了将框架代码和业务代码解耦,代理模式就派上用场了。代理类DMUserControllerProxy和原始类DMUserController实现相同的接口DMUserControllerDelegate。DMUserController类只负责业务功能。代理类DMUserControllerProxy负责在业务代码执行前后附加其他逻辑代码,并通过委托的方式调用原始类类执行业务代码。具体的代码实现如下:
@protocol DMUserControllerDelegate <NSObject>
- (void)userLogin:(NSString *)telephone password:(NSString *)password;
- (void)userRegister:(NSString *)telephone password:(NSString *)password;
@end
@interface DMUserController : NSObject <DMUserControllerDelegate>
@end
@implementation DMUserController
#pragma mark - DMUserControllerDelegate method
- (void)userLogin:(NSString *)telephone password:(NSString *)password
{
// 省略登录逻辑
}
- (void)userRegister:(NSString *)telephone password:(NSString *)password
{
// 省略注册逻辑
}
@end
@interface DMUserControllerProxy : NSObject <DMUserControllerDelegate>
- (instancetype)initWithUserController:(DMUserController *)userController;
@end
@interface DMUserControllerProxy ()
@property (nonatomic, strong) DMUserController *userController;
@property (nonatomic, strong) DMMetricsCollector *metricsCollector;
@end
@implementation DMUserControllerProxy
- (instancetype)initWithUserController:(DMUserController *)userController
{
if (self = [super init]) {
self.userController = userController;
self.metricsCollector = [[DMMetricsCollector alloc] init];
}
return self;
}
#pragma mark - DMUserControllerDelegate method
- (void)userLogin:(NSString *)telephone password:(NSString *)password
{
NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
[self.userController userLogin:telephone password:password];
NSTimeInterval endTime = [[NSDate date] timeIntervalSince1970];
NSTimeInterval responseTime = endTime - startTime;
DMRequestInfo *requestInfo = [[DMRequestInfo alloc] init];
requestInfo.keyWord = @"login";
requestInfo.responseTime = responseTime;
requestInfo.startTime = startTime;
[self.metricsCollector recordRequest:requestInfo];
}
- (void)userRegister:(NSString *)telephone password:(NSString *)password
{
NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
[self.userController userRegister:telephone password:password];
NSTimeInterval endTime = [[NSDate date] timeIntervalSince1970];
NSTimeInterval responseTime = endTime - startTime;
DMRequestInfo *requestInfo = [[DMRequestInfo alloc] init];
requestInfo.keyWord = @"register";
requestInfo.responseTime = responseTime;
requestInfo.startTime = startTime;
[self.metricsCollector recordRequest:requestInfo];
}
@end
使用代码示例如下:
id<DMUserControllerDelegate> userController = [[DMUserControllerProxy alloc] initWithUserController:[[DMUserController alloc] init]];
[userController userLogin:@"159****3273" password:@"123456"];
[userController userRegister:@"159****3273" password:@"123456"];
参照基于接口而非实现编程的设计思想,将原始类对象替换为代理类对象的时候,为了让代码改动尽量少,在刚刚的代理模式的代码实现中,代理类和原始类需要实现相同的接口。但是,如果原始类并没有定义接口,并且原始类代码并不是我们开发维护的(比如它来自一个第三方的类库),我们也没办法直接修改原始类,给它重新定义一个接口。在这种情况下,该如何实现代理模式呢?
对于这种外部类的扩展,一般都是采用继承的方式。让代理类继承原始类,然后扩展附加功能。具体代码如下:
@interface DMUserControllerProxy : DMUserController
@end
@interface DMUserControllerProxy ()
@property (nonatomic, strong) DMMetricsCollector *metricsCollector;
@end
@implementation DMUserControllerProxy
- (instancetype)init {
if (self = [super init]) {
self.metricsCollector = [[DMMetricsCollector alloc] init];
}
return self;
}
- (void)userLogin:(NSString *)telephone password:(NSString *)password
{
NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
[super userLogin:telephone password:password];
NSTimeInterval endTime = [[NSDate date] timeIntervalSince1970];
NSTimeInterval responseTime = endTime - startTime;
DMRequestInfo *requestInfo = [[DMRequestInfo alloc] init];
requestInfo.keyWord = @"login";
requestInfo.responseTime = responseTime;
requestInfo.startTime = startTime;
[self.metricsCollector recordRequest:requestInfo];
}
- (void)userRegister:(NSString *)telephone password:(NSString *)password
{
NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
[super userRegister:telephone password:password];
NSTimeInterval endTime = [[NSDate date] timeIntervalSince1970];
NSTimeInterval responseTime = endTime - startTime;
DMRequestInfo *requestInfo = [[DMRequestInfo alloc] init];
requestInfo.keyWord = @"register";
requestInfo.responseTime = responseTime;
requestInfo.startTime = startTime;
[self.metricsCollector recordRequest:requestInfo];
}
@end
动态代理的原理解析
刚刚的代码实现还是有点问题:
- 1.需要在代理类中,将原始类中的所有方法,都重新实现一遍,并且为每个方法都附加相似的代码逻辑。
- 2.如果要添加的附加功能的类不止一个,需要针对每一个类都创建一个代理类。
如果有50个要添加附加功能的原始类,那就要创建50个对应的代理类。这会导致项目中类的个数成倍增加,增加了代码的维护成本。并且,每个代理类中的代码都有点像模板式的“重复”代码,也增加了不必要的开发成本。这个问题怎么解决呢?
可以使用动态代理来解决这个问题。所谓动态代理就是不事先为每个原始类编写代理类,而是在运行的时候,动态地创建原始类对应的代理类,然后再系统中用代理类替换掉原始类。
@interface DMMetricsCollectorProxy : NSProxy
- (instancetype)initWithTarget:(id)target;
+ (instancetype)proxyWithTarget:(id)target;
@end
@interface DMMetricsCollectorProxy ()
@property (nonatomic, weak) id target;
@property (nonatomic, strong) DMMetricsCollector *metricsCollector;
@end
@implementation DMMetricsCollectorProxy
- (instancetype)initWithTarget:(id)target
{
self.target = target;
self.metricsCollector = [[DMMetricsCollector alloc] init];
return self;
}
+ (instancetype)proxyWithTarget:(id)target
{
return [[DMMetricsCollectorProxy alloc] initWithTarget:target];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
SEL sel = invocation.selector;
if (self.target && [self.target respondsToSelector:sel]) {
NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
[invocation invokeWithTarget:self.target];
NSTimeInterval endTime = [[NSDate date] timeIntervalSince1970];
NSTimeInterval responseTime = endTime - startTime;
DMRequestInfo *requestInfo = [[DMRequestInfo alloc] init];
NSString *className = NSStringFromClass([self.target class]);
NSString *selectorName = NSStringFromSelector(sel);
requestInfo.keyWord = [NSString stringWithFormat:@"%@%@", className, selectorName];
requestInfo.responseTime = responseTime;
requestInfo.startTime = startTime;
[self.metricsCollector recordRequest:requestInfo];
}
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
if (self.target && [self.target respondsToSelector:selector]) {
return [self.target methodSignatureForSelector:selector];
} else {
return nil;
}
}
@end
使用的代码如下
DMUserController *tmpUser = [[DMUserController alloc] init];
id<DMUserControllerDelegate> tmpProxy = (id<DMUserControllerDelegate>)[DMMetricsCollectorProxy proxyWithTarget:tmpUser];
[tmpProxy userLogin:@"159****3273" password:@"123456"];
网友评论