Componentized Architecture for iOS Projects: Analysis of MGJRouter, Protocol, and CTMediator
This article examines the motivations, design patterns, and practical implementations of componentized architectures in iOS development, comparing MGJRouter, Protocol‑based routing, and CTMediator solutions, and provides guidelines for integration, memory management, and dynamic configuration in large‑scale mobile applications.
When a company decides to rewrite a project based on existing product logic, the first challenge is selecting an appropriate architecture; the author and teammates explored componentized architectures to address growing codebases and tangled module dependencies.
The article reviews several high‑quality blogs (Mogujie, casatwy, bang) and discusses the componentization schemes proposed by Mogujie and casatwy, as well as the author’s own design.
MGJRouter Scheme
MGJRouter uses a singleton router that maintains a "URL -> block" registry. Registration example:
[[MGJRouter registerURLPattern:@"mgj://detail" toHandler:^(NSDictionary *routerParameters) {
NSString *uid = routerParameters[@"id"];
}];Invocation example: [MGJRouter openURL:@"mgj://detail?id=404"]; It supports passing parameters via URL query strings or a dictionary, but suffers from hard‑coded keys and potential memory leaks due to block captures.
Protocol Scheme
To avoid hard‑coded URLs, the Protocol scheme registers Class objects for protocols. Registration example:
[ModuleManager registerClass:MGJUserImpl forProtocol:@protocol(MGJUserProtocol)];Clients retrieve the class and invoke methods:
Class cls = [[ModuleManager sharedInstance] classForProtocol:@protocol(MGJUserProtocol)];
id userComponent = [[cls alloc] init];
NSString *userName = [userComponent getUserName];While more type‑safe, it still requires explicit registration and does not support remote routing.
CTMediator (casatwy) Scheme
CTMediator provides a unified performTarget:action:params: API, discovering services at runtime via objc_msgSend. Example of a category method:
- (UIViewController *)CTMediator_viewControllerForDetail {
UIViewController *vc = [self performTarget:kCTMediatorTargetA
action:kCTMediatorActionNativFetchDetailViewController
params:@{ @"key": @"value" }];
return [vc isKindOfClass:[UIViewController class]] ? vc : [[UIViewController alloc] init];
}Although flexible, CTMediator introduces many hard‑coded strings and large categories, making the middleware bulky.
Comparison and Recommendations
MGJRouter is lightweight (<200 lines) and unifies remote and local calls via URL routing.
Protocol scheme adds type safety but increases registration code.
CTMediator offers runtime discovery but suffers from extensive hard‑coding and larger code size.
The author recommends MGJRouter for most component communication needs, while using Protocol or CTMediator only when their specific advantages are required.
Integration Practices
Each component should be an independent Git repository, integrated into the main project via CocoaPods as either source or pre‑compiled frameworks. Resources (images, assets) can be packaged as separate components, and configuration tables (Plist/JSON) manage URL‑to‑component mappings, enabling dynamic feature toggling and remote updates.
Memory management considerations include using weak references inside blocks and understanding that both MGJRouter and Protocol keep their registries in singleton objects, leading to resident memory usage.
Overall, the article provides a comprehensive guide to designing, evaluating, and integrating componentized architectures for large iOS applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
