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

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

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

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()
        }
    }
}

你可能感兴趣的:(iOS 项目设计 Pipe 总分结构 向下派发)