iOS | 关于AppDelegate瘦身的想法与实践

2019-09-30  本文已影响0人  夏浩文

随着开发的迭代升级,不断增加新的功能和职责,AppDelegate中的代码量也不断增长,致使Massive,而精通 OOP 的我们自然会想法子对其瘦身。


AppDelegate

什么是AppDelegate


组合模式

实践环节

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface AppDelegateFactory : NSObject

@property (readonly) NSMutableArray *services;

+ (instancetype)standardFactory;

@end

NS_ASSUME_NONNULL_END
#import "AppDelegateFactory.h"

@interface AppDelegateFactory ()

@property (nonatomic, strong) NSMutableArray *services;

@end

@implementation AppDelegateFactory

+ (instancetype)standardFactory {
    static AppDelegateFactory *insance = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        insance = [AppDelegateFactory new];
    });
    return insance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        makeDefault(self);
    }
    return self;
}

NS_INLINE void makeDefault(AppDelegateFactory *factory) {
    
    [factory registeService:@"ComponentLaunchService"];
//    [factory registeService:@"ComponentPushService"];
//    [factory registeService:@"ComponentBackgroundService"];
}

- (void)registeService:(NSString *)serviceClassString {
    
    Class targetClass = NSClassFromString(serviceClassString);
    NSObject *service = [[targetClass alloc] init];
    
    if (![self.services containsObject:service])
        [self.services addObject:service];
}


#pragma mark - Lazy load

/**
 生命流程中,我们有时候需要保持调用顺序,所以采用数组结构
 
 */
- (NSMutableArray *)services {
    if (!_services) {
        _services = [NSMutableArray array];
    }
    return _services;
}

@end
#import <UIKit/UIKit.h>

@interface ComponentLaunchService : NSObject <UIApplicationDelegate>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

@end
@implementation ComponentLaunchService

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    /// launch your application

    return YES;
}
#import "AppDelegate.h"
#import "AppDelegateFactory.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    id<UIApplicationDelegate> service;
    for(service in [AppDelegateFactory standardFactory].services){
        ///若服务响应
        if ([service respondsToSelector:_cmd]) { 
            [service application:application didFinishLaunchingWithOptions:launchOptions];
        }
    }
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    id<UIApplicationDelegate> service;
    for(service in [AppDelegateFactory standardFactory].services){
        if ([service respondsToSelector:_cmd]){
            [service applicationDidEnterBackground:application];
        }
    }
    //程序进入后台,通知服务器
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    
    id<UIApplicationDelegate> service;
    for(service in [AppDelegateFactory standardFactory].services){
        if ([service respondsToSelector:_cmd]){
            [service applicationWillEnterForeground:application];
        }
    }
    //程序从后台进入前台
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    
    id<UIApplicationDelegate> service;
    for(service in [AppDelegateFactory standardFactory].services){
        if ([service respondsToSelector:_cmd]){
            [service applicationDidReceiveMemoryWarning:application];
        }
    }
    //收到内存警告时
}

///..省略以下生命周期回调方法
@end

中介者模式

实践环节

import UIKit

class AppLifecycleMediator: NSObject {
    private var listeners: [AppLifecycleListener]
    
    init(listeners: [AppLifecycleListener]) {
        self.listeners = listeners
        super.init()
        subscribe()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    private func subscribe() {
        NotificationCenter.default.addObserver(self, selector: #selector(onAppDidFinishLaunching), name: UIApplication.didFinishLaunchingNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(onAppDidReceiveMemoryWarning), name: UIApplication.didReceiveMemoryWarningNotification, object: nil)
    }
    
    @objc private func onAppDidFinishLaunching() {
        listeners.forEach { $0.onAppDidFinishLaunching() }
    }
    
    @objc func onAppDidReceiveMemoryWarning() {
        listeners.forEach { $0.onAppDidReceiveMemoryWarning() }
    }
}
import UIKit

protocol AppLifecycleListener {
    func onAppDidFinishLaunching()
    func onAppDidReceiveMemoryWarning()
}

extension AppLifecycleListener {
    func onAppDidFinishLaunching() {}
    func onAppDidReceiveMemoryWarning() {}
}

//MARK: - Listeners.. (举个栗子)

class SocketListener: AppLifecycleListener {
    func onAppDidFinishLaunching() {
        print("[开启长链接..]")
    }
}
extension AppLifecycleMediator {
    static func makeDefaultMediator() -> AppLifecycleMediator {
        let socketListener = SocketListener()
        return AppLifecycleMediator(listeners: [socketListener])
    }
}
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    // point code
    let mediator = AppLifecycleMediator.makeDefaultMediator()

    ///省略以下内容
}
优点在于

其实我们对AppDelegate都是维系代码可维护度,职责的划分,保证不会在部分修改时牵一发而动全身,这样使得代码更灵活与茁壮,可随时插拔与复用。
以上设计方式亦适用于,复杂模块职能划分,本文仅做简单介绍与思考~😆

上一篇 下一篇

猜你喜欢

热点阅读