Componentized Architecture and Routing Solutions for iOS Mobile Development
This article explains the evolution, analysis, and improvement of componentized architecture in large‑scale iOS projects, compares MGJRouter, Protocol, and CTMediator routing schemes, discusses integration, resource management, memory considerations, and provides practical code examples for implementing modular components.
Origin of Componentized Architecture – As mobile internet expands, codebases and business logic grow, making single‑project MVC insufficient for large apps like Taobao. In 2013 Taobao shifted to a plugin architecture and later to a full componentized architecture to handle rapid business expansion.
Problem Analysis – Large projects suffer from unclear module boundaries, high coupling, and the need to compile the whole project for testing a single feature. Adding a middle layer merely shifts coupling to the layer itself.
Architecture Improvement – Introduce true componentization: each module becomes an independent component integrated by a host project. Benefits include clearer business division, easier onboarding, better maintainability, selective compilation, and simple CocoaPods integration.
MGJRouter Scheme – A lightweight URL‑based router where services are registered as blocks. Example registration and invocation:
#ifndef UserCenterPublicHeader_h
#define UserCenterPublicHeader_h
/** Jump to login */
static const NSString * CTBUCUserLogin = @"CTB://UserCenter/UserLogin";
#endif
[MGJRouter registerURLPattern:@"mgj://detail" toHandler:^(NSDictionary *routerParameters) {
NSString *uid = routerParameters[@"id"];
}];
[MGJRouter openURL:@"mgj://detail?id=404"];Parameters can be passed via URL query strings or dictionaries, allowing non‑string types.
Protocol Scheme – Replaces URL‑block mapping with Protocol‑to‑Class mapping via a ModuleManager. Services are defined in protocols, implemented in MGJComponentProtocol classes, and registered:
[ModuleManager registerClass:MGJUserImpl forProtocol:@protocol(MGJUserProtocol)];
Class cls = [[ModuleManager sharedInstance] classForProtocol:@protocol(MGJUserProtocol)];
id userComponent = [[cls alloc] init];
NSString *userName = [userComponent getUserName];CTMediator Scheme – Provides remote and local invocation via performTarget:action:params: and performActionWithUrl:completion:. Calls are decoupled through runtime discovery, but many hard‑coded strings remain. Example:
- (UIViewController *)CTMediator_viewControllerForDetail {
UIViewController *vc = [self performTarget:kCTMediatorTargetA
action:kCTMediatorActionNativFetchDetailViewController
params:@{ @"key": @"value" }];
return [vc isKindOfClass:[UIViewController class]] ? vc : [[UIViewController alloc] init];
}Integration & Resource Management – Each component is a separate Git repo integrated into the host via CocoaPods, either as source or framework. Resources (images, assets, databases) can be packaged as independent components and referenced through podspecs.
Advantages & Considerations – Componentization improves code reuse, parallel development, testing isolation, and reduces merge conflicts. MGJRouter is lightweight and unifies remote/local calls; Protocol adds type safety but increases registration overhead; CTMediator offers flexibility but can become bulky. Memory usage is affected by singleton registries and block captures, requiring careful weak references.
Conclusion – For iOS mobile projects, a componentized architecture with a lightweight router like MGJRouter is recommended, while Protocol or CTMediator can be used when additional type safety or remote routing is needed.
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.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
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.
