赞
踩
// 命令协议 @protocol Command <NSObject> - (void)execute; @end // 初始化第三方库 @interface InitializeThirdPartiesCommand : NSObject <Command> @end // 初始化主视图 @interface InitializeRootViewControllerCommand : NSObject <Command> @property (nonatomic, strong) UIWindow *keyWindow; @end // 初始化视图全局配置 @interface InitializeAppearanceCommand : NSObject <Command> @end // ...
@implementation StartupCommandsBuilder
// 返回数组,元素为遵守 Command 协议的对象
- (NSArray<id<Command>> *)build {
return @[ [InitializeAppearanceCommand new],
[InitializeRootViewControllerCommand new],
[InitializeThirdPartiesCommand new]];
}
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[[[StartupCommandsBuilder alloc] init] build] enumerateObjectsUsingBlock:^(id<Command> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj execute];
}];
return YES;
}
// 组装类 @interface CompositeAppDelegate : UIResponder <UIApplicationDelegate> + (instancetype)makeDefault; @end @implementation CompositeAppDelegate + (instancetype)makeDefault { // 这里要实现单例 return [[CompositeAppDelegate alloc] init]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[PushNotificationAppDelegate new] application:application didFinishLaunchingWithOptions:launchOptions]; [[ThirdPartiesConfiguratorAppDelegate new] application:application didFinishLaunchingWithOptions:launchOptions]; return YES; } @end
// 叶子类:推送消息处理 @interface PushNotificationAppDelegate : UIResponder <UIApplicationDelegate> @end // 叶子类:初始化第三方库 @interface ThirdPartiesConfiguratorAppDelegate : UIResponder <UIApplicationDelegate> @end @implementation PushNotificationAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSLog(@"PushNotificationAppDelegate"); return YES; } @end @implementation ThirdPartiesConfiguratorAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSLog(@"ThirdPartiesConfiguratorAppDelegate"); return YES; } @end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[CompositeAppDelegate makeDefault] application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
@interface APPLifeCycleMediator : NSObject + (instancetype)makeDefaultMediator; @end @implementation APPLifeCycleMediator { @private NSArray<id<AppLifeCycleListener>> * _listeners; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (instancetype)initWithListeners:(NSArray<id<AppLifeCycleListener>> *)listeners { if (self = [super init]) { _listeners = listeners; // 通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidEnterBackgroud) name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppDidFinishLaunching) name:UIApplicationDidFinishLaunchingNotification object:nil]; } return self; } // 定义好静态类方法,初始化所有监听者 + (instancetype)makeDefaultMediator { static APPLifeCycleMediator * mediator; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ mediator = [[APPLifeCycleMediator alloc] initWithListeners:@[[VideoListener new], [SocketListener new]]]; }); return mediator; } - (void)onAppWillEnterForeground { [_listeners[1] onAppWillEnterForeground]; } - (void)onAppDidEnterBackgroud { [_listeners[0] onAppDidEnterBackgroud]; } - (void)onAppDidFinishLaunching { } @end
// 监听协议 @protocol AppLifeCycleListener <NSObject> @optional - (void)onAppWillEnterForeground; - (void)onAppDidEnterBackgroud; - (void)onAppDidFinishLaunching; @end @interface VideoListener : NSObject <AppLifeCycleListener> @end @interface SocketListener : NSObject <AppLifeCycleListener> @end @implementation VideoListener - (void)onAppDidEnterBackgroud { NSLog(@"停止视频播放"); } @end @implementation SocketListener - (void)onAppWillEnterForeground { NSLog(@"开启长链接"); } @end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[APPLifeCycleMediator makeDefaultMediator];
return YES;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。