獲取CTMediator單例對象
#pragma mark - public methods
+ (instancetype)sharedInstance
{
static CTMediator *mediator;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
mediator = [[CTMediator alloc] init];
});
return mediator;
}
遠程App調用入口
/*
scheme://[target]/[action]?[params]
url sample:
aaa://targetA/actionB?id=1234&title=title
[url query]: id=1234&title=title
[url path]: /actionB
[url host]: targetA
*/
- (id)performActionWithUrl:(NSURL *)url completion:(void (^)(NSDictionary *))completion
{
//url參數的處理
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
//
NSString *urlString = [url query];
for (NSString *param in [urlString componentsSeparatedByString:@"&"]) {
NSArray *elts = [param componentsSeparatedByString:@"="];
if([elts count] < 2) continue;
[params setObject:[elts lastObject] forKey:[elts firstObject]];
}
// 這里這么寫主要是出于安全考慮,防止黑客通過遠程方式調用本地模塊。這里的做法足以應對絕大多數場景,如果要求更加嚴苛,也可以做更加復雜的安全邏輯。
NSString *actionName = [url.path stringByReplacingOccurrencesOfString:@"/" withString:@""];
if ([actionName hasPrefix:@"native"]) {
return @(NO);
}
// 這個demo針對URL的路由處理非常簡單,就只是取對應的target名字和method名字,但這已經足以應對絕大部份需求。如果需要拓展,可以在這個方法調用之前加入完整的路由邏輯
id result = [self performTarget:url.host action:actionName params:params shouldCacheTarget:NO];
if (completion) {
if (result) {
completion(@{@"result":result});
} else {
completion(nil);
}
}
return result;
}
本地組件調用入口
/**
* 本地組件調用入口
*
* @param targetName 類對象 OC中類對象是要Target_為前綴的
* @param actionName 方法名稱 最后實際調用的是以Action_為前綴的
* @param params 參數
* @param shouldCacheTarget 是否緩存拼接后的類對象
*
* @return return value JSon格式的字符串
*/
- (id)performTarget:(NSString *)targetName action:(NSString *)actionName params:(NSDictionary *)params shouldCacheTarget:(BOOL)shouldCacheTarget
{
//供swift項目使用
NSString *swiftModuleName = params[kCTMediatorParamsKeySwiftTargetModuleName];
// generate target
NSString *targetClassString = nil;
if (swiftModuleName.length > 0) {
targetClassString = [NSString stringWithFormat:@"%@.Target_%@", swiftModuleName, targetName];
} else {
// 拼裝類字符串
targetClassString = [NSString stringWithFormat:@"Target_%@", targetName];
}
//先從緩存中取對象
NSObject *target = self.cachedTarget[targetClassString];
if (target == nil) {
//不存在直接根據字符串創建類,并且初始化對象
Class targetClass = NSClassFromString(targetClassString);
target = [[targetClass alloc] init];
}
// 拼裝方法字符串
NSString *actionString = [NSString stringWithFormat:@"Action_%@:", actionName];
// 生成SEL
SEL action = NSSelectorFromString(actionString);
//先從緩存取,取不到去創建,但是也有可能創建失敗的情況(targetName值不正確)
if (target == nil) {
// 這里是處理無響應請求的地方之一,這個demo做得比較簡單,如果沒有可以響應的target,就直接return了。實際開發過程中是可以事先給一個固定的target專門用于在這個時候頂上,然后處理這種請求的
[self NoTargetActionResponseWithTargetString:targetClassString selectorString:actionString originParams:params];
return nil;
}
// 是否緩存該對象
if (shouldCacheTarget) {
self.cachedTarget[targetClassString] = target;
}
// 該對象是否能響應調起該方法
if ([target respondsToSelector:action]) {
return [self safePerformAction:action target:target params:params];
} else {
// 這里是處理無響應請求的地方,如果無響應,則嘗試調用對應target的notFound方法統一處理
SEL action = NSSelectorFromString(@"notFound:");
if ([target respondsToSelector:action]) {
return [self safePerformAction:action target:target params:params];
} else {
// 這里也是處理無響應請求的地方,在notFound都沒有的時候,這個demo是直接return了。實際開發過程中,可以用前面提到的固定的target頂上的。
[self NoTargetActionResponseWithTargetString:targetClassString selectorString:actionString originParams:params];
[self.cachedTarget removeObjectForKey:targetClassString];
return nil;
}
}
}
注意:想要調用此方法,你的類必須是
Target_
為前綴的,而方法必須是Action_
為前綴的,另外代碼中對于處理無響應的情況分了兩種情況:
- target == nil 調用
NoTargetActionResponseWithTargetString
方法- action無法響應的時候,會先去響應
notFound
方法
如果notFound
方法無法響應,依然會去調用NoTargetActionResponseWithTargetString
方法
無響應事件處理
上述代碼中曾提到在實際開發過程中是可以事先給一個固定的target專門用于在無響應的時候頂上,然后處理這種請求。
#pragma mark - private methods
- (void)NoTargetActionResponseWithTargetString:(NSString *)targetString selectorString:(NSString *)selectorString originParams:(NSDictionary *)originParams
{
SEL action = NSSelectorFromString(@"Action_response:");
NSObject *target = [[NSClassFromString(@"Target_NoTargetAction") alloc] init];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
params[@"originParams"] = originParams;
params[@"targetString"] = targetString;
params[@"selectorString"] = selectorString;
[self safePerformAction:action target:target params:params];
}
注意:代碼中
Target_NoTargetAction
就是用來統一處理無響應時給的固定的target
,Action_response
就是這個類用來調用的方法。
@interface Target_NoTargetAction : NSObject
- (void)Action_response:(NSDictionary *)params;
@end
調用代碼解析
- (id)safePerformAction:(SEL)action target:(NSObject *)target params:(NSDictionary *)params
{
//通過實例獲取某一個方法簽名:
NSMethodSignature* methodSig = [target methodSignatureForSelector:action];
if(methodSig == nil) {
return nil;
}
// 獲取方法返回值類型
const char* retType = [methodSig methodReturnType];
NSLog(@"返回值的類型 %s",retType);
if (strcmp(retType, @encode(void)) == 0) {
// 通過NSMethodSignature對象創建NSInvocation對象,NSMethodSignature為方法簽名類
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig];
// 設置消息參數
[invocation setArgument:¶ms atIndex:2];
// 設置要調用的消息
[invocation setSelector:action];
// 設置消息調用者
[invocation setTarget:target];
// 發送消息,即執行方法
[invocation invoke];
return nil;
}
if (strcmp(retType, @encode(NSInteger)) == 0) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig];
[invocation setArgument:¶ms atIndex:2];
[invocation setSelector:action];
[invocation setTarget:target];
[invocation invoke];
NSInteger result = 0;
[invocation getReturnValue:&result];
return @(result);
}
if (strcmp(retType, @encode(BOOL)) == 0) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig];
[invocation setArgument:¶ms atIndex:2];
[invocation setSelector:action];
[invocation setTarget:target];
[invocation invoke];
BOOL result = 0;
[invocation getReturnValue:&result];
return @(result);
}
if (strcmp(retType, @encode(CGFloat)) == 0) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig];
[invocation setArgument:¶ms atIndex:2];
[invocation setSelector:action];
[invocation setTarget:target];
[invocation invoke];
CGFloat result = 0;
[invocation getReturnValue:&result];
return @(result);
}
if (strcmp(retType, @encode(NSUInteger)) == 0) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig];
[invocation setArgument:¶ms atIndex:2];
[invocation setSelector:action];
[invocation setTarget:target];
[invocation invoke];
NSUInteger result = 0;
[invocation getReturnValue:&result];
return @(result);
}
// 當你確定編譯器的警告對你來說沒有什么用處的時候,為了避免心煩,
// 你可以使用#pragma clang diagnostic ignored “xxx” 這樣的語句來忽略掉相應的警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [target performSelector:action withObject:params];
#pragma clang diagnostic pop
}
清除緩存
- (void)releaseCachedTargetWithTargetName:(NSString *)targetName
{
NSString *targetClassString = [NSString stringWithFormat:@"Target_%@", targetName];
[self.cachedTarget removeObjectForKey:targetClassString];
}
總結
中間者架構采用中間者統一管理的方式,來控制app的整個生命周期中組件間的調用關系。拆分的組件都會依賴于中間者,但是組間之間就不存在相互依賴的關系了。由于其他組件都會依賴于這個中間者,相互間的通信都會通過中間者統一調度,所以組件間的通信也就更容易管理了。在中間者上也能夠輕松添加新的設計模式。中間者架構的易管控帶來的架構更穩固,易擴展帶來的靈活性,從而使得架構更容易擴展。