iOS 项目设计 Pipe 总分结构 向下派发

2021-10-12  本文已影响0人  松龄学编程

设计思路:总分结构,向下派发指令,减少下层逻辑耦合,下层逻辑都与上层逻辑联系,多管线设计,每只管线单一指责

解决问题:通知管线,解决通知多处注册的问题。可以做到一处监听,向下派发指令,避免了多处注册,代码乱,不易维护的问题。分页管线,管理分页,减少下层跳转耦合。

class Pipe {
    static let shared = Pipe()
    
    let tab = TabPipe()
    let basicData = BasicDataPipe()
}

class TabPipe {
    func jumpTo(index: Int) {
        (keyWindow()?.rootViewController as? MainTabViewController)?.selectedIndex = index
    }
}

protocol BasicDataPipeDelegate {
    func refreshDataAfterBasicDataFetched()
}

class BasicDataPipe {
    private var deliveries: [BasicDataPipeDelegate] = []
    
    init() {
        NotificationCenter.default.addObserver(forName: .basicDataDidSetup, object: nil, queue: .current) { [weak self] (notification) in
            guard let `self` = self else { return }
            self.flash()
        }
    }
    
    func register(_ delivery: BasicDataPipeDelegate) {
        deliveries.append(delivery)
    }
    
    private func flash() {
        for d in deliveries {
            d.refreshDataAfterBasicDataFetched()
        }
    }
}

上一篇下一篇

猜你喜欢

热点阅读