【译】Swift World:设计模式--中介者模式

原文:https://medium.com/swiftworld/swift-world-design-patterns-mediator-e6b3c35d68b0 作者:Peng

今天我们讨论一下中介者模式(Mediator)。这次不从抽象定义开始,而是用现实世界中的一个场景来解释它。在一个团队里,有产品经理,开发工程师,质量工程师。当开发完成了某些功能,将代码提交到仓库。相关环节人员,像质量工程师和产品经理需要被通知。

protocol Collogue {
    var id: String { get }
    func send(message: String)
    func receive(message: String)
}
class Developer: Collogue {
    var id: String
    var qe: QE
    var pm: PM
    init(qe: QE, pm: PM) {
        self.id = "Developer"
        self.qe = qe
        self.pm = pm
    }
    func send(message: String) {
        qe.receive(message: message)
        pm.receive(message: message)
    }
    func receive(message: String) {
        print(message)
    }   
}
class QE: Collogue {
    var id: String
    var developer: Developer
    var pm: PM
    init(developer: Developer, pm: PM) {
        self.id = "QE"
        self.developer = developer
        self.pm = pm
    }
    func send(message: String) {
        developer.receive(message: message)
        pm.receive(message: message)
    }
    func receive(message: String) {
        print(message)
    }
}
class PM: Collogue {
    var id: String
    var developer: Developer
    var qe: QE
    init(developer: Developer, qe: QE) {
        self.id = "PM"
        self.developer = developer
        self.qe = qe
    }
    func send(message: String) {
        developer.receive(message: message)
        qe.receive(message: message)
    }
    func receive(message: String) {
        print(message)
    }
}

每个角色都需要持有另一个角色的实例,这种连接方式是高耦合的,且很不容易修改。

现在我们需要一个中介者帮助我们简化这个系统。中介者的目的是帮助对象之间相互交流。它让每个对象都是跟自己进行交互而不是其他对象。当前对象不需要持有别的对象,而是持有中介者。这样将解耦系统,它的结构图如下所示:

我们来写一下代码:

protocol Mediator {
    func send(message: String, sender: Colleague)
}
class TeamMediator: Mediator {
    var colleagues: [Colleague] = []
    func register(colleague: Colleague) {
        colleagues.append(colleague)
    }
    func send(message: String, sender: Colleague) {
        for colleague in colleagues {
            if colleague.id != sender.id {
                colleague.receive(message: message)
            }
        }
    }
}

通过持有中介者,那几个角色对象变成了这样:

protocol Colleague {
    var id: String { get }
    var mediator: Mediator { get }
    func send(message: String)
    func receive(message: String)
}
class Developer: Colleague {
    var id: String
    var mediator: Mediator
    init(mediator: Mediator) {
        self.id = "Developer"
        self.mediator = mediator
    }
    func send(message: String) {
        mediator.send(message: message, sender: self)
    }
    func receive(message: String) {
        print("Developer received: " + message)
    }
}
class QE: Colleague {
    var id: String
    var mediator: Mediator
    init(mediator: Mediator) {
        self.id = "QE"
        self.mediator = mediator
    }
    func send(message: String) {
        mediator.send(message: message, sender: self)
    }
    func receive(message: String) {
        print("QE received: " + message)
    }
}
class PM: Colleague {
    var id: String
    var mediator: Mediator
    init(mediator: Mediator) {
        self.id = "PM"
        self.mediator = mediator
    }
    func send(message: String) {
        mediator.send(message: message, sender: self)
    }
    func receive(message: String) {
        print("PM received: " + message)
    }
}

这样一来,整个结构就变成了下面这样:

让我们用新的方式来使用它:

//usage
let mediator = TeamMediator()
let qe = QE(mediator: mediator)
let developer = Developer(mediator: mediator)
let pm = PM(mediator: mediator)
mediator.register(colleague: developer)
mediator.register(colleague: qe)
mediator.register(colleague: pm)
mediator.send(message: "Hello world!", sender: developer)

另一个相似的例子就是非常受欢迎的Notification(NSNotification)。你可以在网上找到很多相关的代码。