Design Scalable iOS Component Architecture: MGJRouter, CTMediator & More
This article explores the principles and practical implementation of component-based architecture for iOS applications, comparing MGJRouter, CTMediator, and Protocol approaches, detailing integration methods, memory management, routing, dynamic configuration, and lessons from industry leaders like Mogujie, Didi, and Alibaba.
Introduction
The article provides a comprehensive guide to building a component‑based architecture for iOS projects, focusing on modularization, routing, and dynamic configuration. It analyses three popular solutions—MGJRouter, Protocol‑based routing, and CTMediator—while referencing real‑world implementations from Mogujie, Didi, and Alibaba.
Componentization Overview
Componentization aims to split a large codebase into independent modules (components) that can be developed, tested, and released separately. Each component is managed in its own Git repository and integrated into the main app via CocoaPods, allowing parallel development and reducing merge conflicts.
MGJRouter Scheme
MGJRouter is a lightweight URL‑based router that maps a URL to a block of code. It supports both native and H5 navigation, flexible parameter passing, and optional completion callbacks.
#ifndef UserCenterPublicHeader_h
#define UserCenterPublicHeader_h
/** Jump to user login screen */
static const NSString *CTBUCUserLogin = @"CTB://UserCenter/UserLogin";
static const NSString *CTBUCUserRegister = @"CTB://UserCenter/UserRegister";
static const NSString *CTBUCUserStatus = @"CTB://UserCenter/UserStatus";
#endifRegistration example:
[MGJRouter registerURLPattern:@"CTB://UserCenter/UserLogin" toHandler:^(NSDictionary *routerParameters) {
UIViewController *loginVC = [[LoginViewController alloc] init];
loginVC.delegate = routerParameters[CTBUserCenterLoginDelegateKey];
return loginVC;
}];Protocol Scheme
The Protocol approach replaces URL‑to‑block mapping with a Class‑to‑Protocol mapping. A central ModuleManager stores a dictionary that maps a protocol to the implementing class, allowing decoupled calls via [[class alloc] init] after runtime lookup.
@protocol MGJUserProtocol <NSObject>
- (NSString *)getUserName;
@end
@interface MGJUserImpl : NSObject <MGJUserProtocol>
@end
@implementation MGJUserImpl
- (NSString *)getUserName { return @"DemoUser"; }
@end
[ModuleManager registerClass:[MGJUserImpl class] forProtocol:@protocol(MGJUserProtocol)];CTMediator Scheme
CTMediator provides a centralized mediator that forwards calls to target‑action pairs using runtime discovery. It reduces direct dependencies but can become bulky when many services are added.
- (UIViewController *)CTMediator_viewControllerForDetail {
UIViewController *vc = [self performTarget:@"A" action:@"nativeFetchDetailViewController" params:@{@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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
