Swift開發-組件化開發之AppDelegate分解

如今不少的大型項目基本都是多人協做的,基本都是以組件化開發爲主,減小不一樣開發人員之間的相互影響,小中型項目有沒有必須使用呢,有人說不必,殺雞焉用牛刀,實際上是有必需要的,雖然代碼量有增長,可是最起碼代碼結構更清晰,也利於代碼維護,若是去作大型項目也能提前適應,再者也是對編碼能力的提高,好處不少啊。web

下面從swift開發iOS爲例來作個介紹。swift

AppDelegate是iOS開發中一個很重要的類,不少系統的事件處理都在這個類下,如推送,分享,支付等等,這個類加太多的處理會致使很臃腫,有人建議說能夠用分類,這也是一種辦法,設計模式

分類有一個很差的地方就是會致使代碼分散,閱讀性會差一些。從設計模式的6大原則來解決問題,能夠有其它的解決方案。AppDelegate下相關的代理仍是寫在其下,AppDelegate只作粘合做用,其它不一樣的業務須要單獨處理,定義到本身的業務類中。api

項目中的MCHKit文件夾只作共用代碼包,與業務無關,拿到任何項目中均可以拿來直接使用,並不會報錯。下面以推送功能爲例.app

在項目的MCHKit下建立 ModuleManager,RemotePushMoudel類,包含一個ModuleManagerDelegate 協議,業務類須要實現ModuleManagerDelegate協議,做爲業務組件和AppDlegate通訊的橋樑。ide

全部組件的基類組件化

 

 

具體實現,定義一個ModuleManager.swiftfetch

//  SwfitDemo
//  Created by menchao on 2018/9/26.
//  Copyright © 2018年 cedarhd. All rights reserved.

import UIKit

@objc public protocol ModuleManagerDelegate {

    @objc optional func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey : Any]?)

   //其它的處理。。。

    func applicationWillResignActive(_ application: UIApplication)

    func applicationDidEnterBackground(_ application: UIApplication)
}

public class ModuleManager : NSObject{
    static  let sharedInstace = ModuleManager.init()
    private override init(){

    }
    public func loadModule(_ module: ModuleManagerDelegate? ){
        if((module) != nil){
            self.allModules.append(module!)
        }
    }
    public func loadAllModule(_ modules:[Array<ModuleManagerDelegate>]?){
        if((modules) != nil){
            self.allModules.removeAll()
            for item in modules!{
                self.allModules.append(item as! ModuleManagerDelegate)
            }
        }
    }
    // MARK: - app delegate
    public  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey : Any]?){
        for  service in self.allModules {
            let serviceDelegate  =   service
            if serviceDelegate.application != nil{
                serviceDelegate.application!(application, didFinishLaunchingWithOptions: launchOptions)
            }
        }
    }

    lazy var allModules: Array<ModuleManagerDelegate> = {
        var array = Array<ModuleManagerDelegate>()
        return array
    }()
}

 

ModuleManagerDelegate裏的名稱能夠改成本身的名稱,爲了和系統相呼應,建議和系統保持同樣的名字。編碼

 定義一個推送消息模塊RemotePushMoudel繼承ModuleManagerDelegate,用來處理協議方法。以下,把推送相關的處理用extension定義,減小AppDelegate的臃腫import UIKitspa

class RemotePushMoudel:NSObject, ModuleManagerDelegate {
    var delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?){
        print("TestRemotePush 實現 application")
        self.registerAppNotificationSettings(launchOptions: launchOptions)
    }
func registerAppNotificationSettings(launchOptions: [UIApplicationLaunchOptionsKey : Any]?) { if #available(iOS 10.0, *) { let notifiCenter = UNUserNotificationCenter.current() notifiCenter.delegate = self let types = UNAuthorizationOptions(arrayLiteral: [.alert, .badge, .sound]) notifiCenter.requestAuthorization(options: types) { (flag, error) in if flag { print("iOS request notification success") }else{ print(" iOS 10 request notification fail") } } } else { let setting = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) UIApplication.shared.registerUserNotificationSettings(setting) } UIApplication.shared.registerForRemoteNotifications() } // MARK: - RemoteNotification @available(iOS 10.0, *) func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void){ } @available(iOS 10.0, *) public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void){ } }

  

定義一個業務類PushBusiness,這個類能夠專門處理推送相關的業務邏輯,實現以下:

import UserNotifications

// MARK: - push extension
extension AppDelegate{
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("deviceToken: \(deviceToken)")
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error){

    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]){

        //方法1.此到處理推送業務邏輯 具體的業務實現

     //do something

//方法2. 爲了減小耦合,也能夠把業務邏輯單獨定義實現
     PushBusiness.oneBusinessReceiveRemotePush(userInfo: userInfo) } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { } }
//業務處理
class PushBusiness: NSObject {

    class  func oneBusinessReceiveRemotePush(userInfo: [AnyHashable : Any]) {
        print("userInfo: \(userInfo)")
//處理具體推送業務 } }

定義完了以上,下一步用一個類管理全部的業務,定義 ServiceComponentManager 以下:

import UIKit
public class ServiceComponentManager {
public class func registerAllService(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey : Any]?){ /// add all Moudel //push let pushModule = RemotePushMoudel() ModuleManager.sharedInstace.loadModule(pushModule)     //添加其它 //pay //other ModuleManager.sharedInstace.application(application,didFinishLaunchingWithOptions: launchOptions) } }

以上定義好了後,直接在AppDelegate的入口方法添加便可。

class AppDelegate: UIResponder, UIApplicationDelegate ,UNUserNotificationCenterDelegate{
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = UIColor.white
        self.window?.rootViewController = RootTabBarController()
        self.window?.makeKeyAndVisible()

        //註冊服務模塊
        ServiceComponentManager.registerAllService(application, didFinishLaunchingWithOptions: launchOptions)
        return true
    }
}

經過以上說明,能夠作到對AppDelegate組件化分解,減小AppDelegate的臃腫,這樣定義耦合性就很低了,不一樣的業務互不干擾,方便維護。若是項目須要增長如分享,支付等,均可以使用相似方法,若是想在不一樣的項目中複用RemotePushMoudel,還能夠在改造,RemotePushMoudel放到MCHKit中,業務代碼抽離到業務模塊類,能夠經過通知或delegate等來實現。 雖然代碼量稍有增長,可是可讀性更強,同時也作到了高內聚,低耦合。

相關文章
相關標籤/搜索