模块化AppDelegate

作为了iOS project自带的life cycle管理类,同时也是原生的一个singleton类,AppDelegate其实很多时候是简直就是bad code的典范。很多人会直接把所有需要持续性存在的方法和属性全部塞到这个类里面,使得很多项目的AppDelegate都有大几白行甚至上千行,这个给代码管理和迭代带来了巨大的困难。作为Tech Lead解决这个问题的方法就是强迫大家遵循一定的设计原则,在实际工作中,我提出的原则就是:“谁污染谁治理” - 每一个模块或者方法的作者有义务自行管理模块或者方法的life cycle。具体实现方法就是通过将跟App Life Cycle相关的方法和模块都抽象成不同的服务来插入原有的AppDelegate之中。这样的好处是代码管理和清理难度大幅度降低,同时代码的可读性大幅度上升。

首先设立一个placeholder来继承UIApplicationDelegate

import UIKit

protocol ApplicationService:UIApplicationDelegate {}

class LifeCycleService:UIResponder,ApplicationService {
    
    var window: UIWindow?
    
    //Why computed? For AppDelegate to override. It should not be computed property if you don't adopt this structure.
    var services:[ApplicationService] {
        return []
    }
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        for service in services {
            service.applicationDidFinishLaunching?(application)
        }
        return true
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        for service in services {
            service.applicationWillResignActive?(application)
        }
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        for service in services {
            service.applicationDidEnterBackground?(application)
        }
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
        for service in services {
            service.applicationWillEnterForeground?(application)
        }
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        for service in services {
            service.applicationDidBecomeActive?(application)
        }
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        for service in services {
            service.applicationWillTerminate?(application)
        }
    }
    /* To be continued...
       I know you don't like the word technologically, so theoretically, you should (another bad word here) implement all methods in the UIApplicationDelegate!!! Why I don't do it? Yes, you are right, because I am lazy. Although lazy is a good word to describe a developer.
     */
}

这只是一个例子,实际上,你可以根据自己需要,把所有UIApplicationDelegate里面的方法全部用这种方法实现。

然后再根据自己的需求来设立想要的Service模块:

import UIKit

class DependencyLoading:NSObject, ApplicationService {
    
    func applicationDidFinishLaunching(_ application: UIApplication) {
        //Let's pretend to have a Reachability class
        Reachability.shared.checkInternetAccessbility()

    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        Reachability.shared.stopMonitotingReachability()
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        Reachability.shared.startMonitoringReachability()
    }
}

extension DependencyLoading:ReachabilityDelegate {
    
    func didLostInternetConnection() {
        /*
         Please Remember this name, next time anything breaks including you forgot your AD-ENT password, make sure you call 
         this guy before step 2.
         */
        print("Siri, call Andrew Martinez...")
    }
}

你可以用这种方法把各种不同的功能抽象成不同的模块,比如Push Notification 相关的方法和属性就可以抽象成NotificationService。LocalNotification的方法抽象成LocationNotificationService。CoreData相关的方法和属性抽象成PersistentService之类的。

最后我们来看一下系统自带的AppDelegate:

import UIKit

@UIApplicationMain
class AppDelegate: LifeCycleService {
    override var services: [ApplicationService] {
        return [    /*
                       Here is checkin list and checkin is mandatory for every one!
                    */
                    LifeCycleService(),
                    DependencyLoading()
               ]
    }
}

你可能感兴趣的:(模块化AppDelegate)