原文:medium.com/swiftworld/…swift
做者:Pengapp
今天咱們討論一下中介者模式(Mediator)。此次不從抽象定義開始,而是用現實世界中的一個場景來解釋它。在一個團隊裏,有產品經理,開發工程師,質量工程師。當開發完成了某些功能,將代碼提交到倉庫。相關環節人員,像質量工程師和產品經理須要被通知。ui
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)
}
}
複製代碼
每一個角色都須要持有另外一個角色的實例,這種鏈接方式是高耦合的,且很不容易修改。 spa
如今咱們須要一箇中介者幫助咱們簡化這個系統。中介者的目的是幫助對象之間相互交流。它讓每一個對象都是跟本身進行交互而不是其餘對象。當前對象不須要持有別的對象,而是持有中介者。這樣將解耦系統,它的結構圖以下所示: code
咱們來寫一下代碼:cdn
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)
}
}
複製代碼
這樣一來,整個結構就變成了下面這樣: blog
讓咱們用新的方式來使用它:開發
//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
)。你能夠在網上找到不少相關的代碼。get