Teacher Casa’s scheme was mainly based on Mediator mode and target-Action mode, and runtime was used to complete the call.

The following uses Demo as an example to analyze the implementation of componentization. First take a look at the Demo project structure:

The CTMediator class is the middleware class that handles and distributes calls between remote and local components.

#import <UIKit/UIKit.h>extern NSString * const kCTMediatorParamsKeySwiftTargetModuleName; @interface CTMediator : NSObject + (instancetype)sharedInstance; - (id)performActionWithUrl:(NSURL *)url completion:(void(^)(NSDictionary *info))completion; - (id)performTarget:(NSString *)targetName action:(NSString *)actionName params:(NSDictionary *)params shouldCacheTarget:(BOOL)shouldCacheTarget; - (void)releaseCachedTargetWithTargetName:(NSString *)targetName; @endCopy the code

The remote call function performActionWithUrl: completion: is parsed by the url processing conversion cost to call.

Take a look at the code implementation of the locally called function:

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];
    }

    // generate action
    NSString *actionString = [NSString stringWithFormat:@"Action_%@:", actionName];
    SEL action = NSSelectorFromString(actionString);
    
    if(target == nil) {// This is one of the places to handle unresponsive requestsreturn. In practice, you can give a fixed target in advance to be used at this time, Then process this request of [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{// This is where the unresponsive request is handled. If there is no response, try calling the notFound method corresponding to target to handle SEL action = NSSelectorFromString(@)"notFound:");
        if ([target respondsToSelector:action]) {
            return [self safePerformAction:action target:target params:params];
        } else{// This is also where unresponsive requests are handled. In the absence of notFound, the demo is directreturn. In practice, you can use the fixed target above mentioned. [self NoTargetActionResponseWithTargetString:targetClassString selectorString:actionString originParams:params]; [self.cachedTarget removeObjectForKey:targetClassString];returnnil; }}Copy the code

First get the Target instance. It then generates the SEL action message that Target wants to send, and then sends the message (calling the function).

The DemoModule folder represents A specific component (module), AModule for short. The target-A class is the implementation of the callable methods (external interfaces) exposed by this component. So what happens when someone else wants to call AModule? It does not refer directly to Target-a, which would then be coupled to the AModule, breaking the component’s independence. Since Mediator mode is Mediator mode, of course there is middleware, let’s look at the CTMediator+CTMediatorModuleAActions class. Classes are used to extend CTMediator, increasing the external interface of AModule and ensuring the independence of AModule. If a BModule is generated, create a CTMediator+CTMediatorModuleBActions to implement the external interface of the BModule.

Personally, I think casA’s componentization scheme is superior to mogujie’s componentization scheme in general. However, the mogustreet URLProtocol method to be modified, using the way of runtime call, and casA scheme is close to the same, you can try. If openURL only supports local calls, register the URL in target-x, which is similar to target-action mode.

If my article is of any help to you, please let me know in the comments, Thanks!