行為模式-責(zé)任鏈模式(The Chain of Responsibility Pattern)

本文大部分內(nèi)容翻譯至《Pro Design Pattern In Swift》By Adam Freeman癌瘾,一些地方做了些許修改诀诊,并將代碼升級(jí)到了Swift2.0,翻譯不當(dāng)之處望多包涵。

責(zé)任鏈模式(The Chain of ResponsibilityPattern)

在責(zé)任鏈模式里藐握,很多對(duì)象由每一個(gè)對(duì)象對(duì)其下家的引用而連接起來形成一條鏈。請(qǐng)求在這個(gè)鏈上傳遞,直到鏈上的某一個(gè)對(duì)象決定處理此請(qǐng)求袜蚕。發(fā)出這個(gè)請(qǐng)求的客戶端并不知道鏈上的哪一個(gè)對(duì)象最終處理這個(gè)請(qǐng)求,這使得系統(tǒng)可以在不影響客戶端的情況下動(dòng)態(tài)地重新組織和分配責(zé)任绢涡。


示例工程

Xcode Command Line Tool 工程:

Message.swift

struct Message {
    let from:String
    let to:String
    let subject:String
}

我們定義了一個(gè)結(jié)構(gòu)體Message牲剃,接下來我們定義發(fā)送Message的Transmitters:

Transmitters.swift

class LocalTransmitter {
    func sendMessage(message: Message) {
        print("Message to \(message.to) sent locally")
    }
}

class RemoteTransmitter {
    func sendMessage(message: Message) {
        print("Message to \(message.to) sent remotely")
    }
}

可以看出我們定義了兩個(gè)代表發(fā)送信息的類,一個(gè)本地和一個(gè)遠(yuǎn)程垂寥。它們都有一個(gè)sendMessage方法颠黎,用來模擬發(fā)送信息另锋。

main.swift

let messages = [
    Message(from: "bob@example.com", to: "joe@example.com",subject: "Free for lunch?"),
    Message(from: "joe@example.com", to: "alice@acme.com",subject: "New Contracts"),
    Message(from: "pete@example.com", to: "all@example.com",subject: "Priority: All-Hands Meeting")
]

let localT = LocalTransmitter()
let remoteT = RemoteTransmitter()

for msg in messages {
if let index = msg.from.characters.indexOf("@"){
    if (msg.to.hasSuffix(msg.from[Range<String.Index>(start:
    index, end: msg.from.endIndex)])) {
    localT.sendMessage(msg)
} else {
    remoteT.sendMessage(msg)
    }
} else {
    print("Error: cannot send message to \(msg.from)")
    }
}

可以看到如果from和to如果有相同的后綴,那么就調(diào)用本地發(fā)送方法狭归;如果后綴不同夭坪,則調(diào)用遠(yuǎn)程發(fā)送方法。運(yùn)行程序过椎,得到下面的輸出:

Message to joe@example.com sent locally
Message to alice@acme.com sent remotely
Message to all@example.com sent locally

理解責(zé)任鏈模式解決的問題

示例中存在的問題是使用transmitter類來發(fā)送消息的組件必須知道應(yīng)該用哪一個(gè)類發(fā)送(本地還是遠(yuǎn)程)室梅。如果要增加新的發(fā)送方式,這將變得十分困難疚宇。假如我們要增加一個(gè)優(yōu)先發(fā)送方式亡鼠,那么將作如下十分蛋疼的修改:

Transmitters.swift

class LocalTransmitter {
    func sendMessage(message: Message) {
        print("Message to \(message.to) sent locally")
    }
}

class RemoteTransmitter {
    func sendMessage(message: Message) {
        print("Message to \(message.to) sent remotely")
    }
}

class PriorityTransmitter {
    func sendMessage(message: Message) {
        print("Message to \(message.to) sent as priority")
    }
}

main.swift

let messages = [
    Message(from: "bob@example.com", to: "joe@example.com",
    subject: "Free for lunch?"),
    Message(from: "joe@example.com", to: "alice@acme.com",
    subject: "New Contracts"),
    Message(from: "pete@example.com", to: "all@example.com",
    subject: "Priority: All-Hands Meeting"),
]

let localT = LocalTransmitter()
let remoteT = RemoteTransmitter()
let priorityT = PriorityTransmitter()

for msg in messages {
    if (msg.subject.hasPrefix("Priority")) {
        priorityT.sendMessage(msg)
    }else if let index = msg.from.characters.indexOf("@"){
        if (msg.to.hasSuffix(msg.from[Range<String.Index>(start:
            index, end: msg.from.endIndex)])) {
            localT.sendMessage(msg)
        } else {
            remoteT.sendMessage(msg)
        }
    } else {
        print("Error: cannot send message to \(msg.from)")
    }
}

運(yùn)行程序:

Message to joe@example.com sent locally
Message to alice@acme.com sent remotely
Message to all@example.com sent as priority

理解責(zé)任鏈模式

責(zé)任鏈模式通過將所有的transmitters 放在一個(gè)鏈里面來解決這個(gè)問題。每一個(gè)環(huán)都在鏈里并且檢查Message對(duì)象來決定是否承擔(dān)責(zé)任敷待。如果鏈里的某一個(gè)環(huán)能夠處理Message對(duì)象间涵,那么它處理。如果不能榜揖,請(qǐng)求傳遞到下一個(gè)環(huán)勾哩。這個(gè)過程一直持續(xù)到有環(huán)處理或者到了鏈中最后一個(gè)環(huán)。



實(shí)現(xiàn)責(zé)任鏈模式

這里我們用定義一個(gè)擁有可選類型屬性來代表鏈中下一個(gè)環(huán)的基類举哟。

Transmitters.swift

class Transmitter {
    var nextLink:Transmitter?
    
    required init() {}
    
    func sendMessage(message:Message) {
        if (nextLink != nil) {
            nextLink!.sendMessage(message);
        } else {
            print("End of chain reached. Message not sent");
        }
    }
    
    private class func matchEmailSuffix(message:Message) -> Bool {
        if let index = message.from.characters.indexOf("@") {
            return message.to.hasSuffix(message.from[Range<String.Index>(start:
                index, end: message.from.endIndex)]);
        }
        return false
    }
}

class LocalTransmitter : Transmitter {
    override func sendMessage(message: Message) {
        if (Transmitter.matchEmailSuffix(message)) {
            print("Message to \(message.to) sent locally")
        } else {
            super.sendMessage(message)
        }
    }
}

class RemoteTransmitter : Transmitter {
    override func sendMessage(message: Message) {
        if (!Transmitter.matchEmailSuffix(message)) {
            print("Message to \(message.to) sent remotely")
        } else {
            super.sendMessage(message)
        }
    }
}

class PriorityTransmitter : Transmitter {
    override func sendMessage(message: Message) {
        if (message.subject.hasPrefix("Priority")) {
            print("Message to \(message.to) sent as priority")
        } else {
            super.sendMessage(message)
        }
    }
}

Transmitter 類定義了發(fā)送器的基本行為思劳,包括了提交請(qǐng)求到鏈中下一個(gè)環(huán)。初始化方法前面的required關(guān)鍵字使得我們可以用類型來創(chuàng)建 transmitter實(shí)例妨猩。

創(chuàng)建和提供責(zé)任鏈

下一步我們將創(chuàng)建責(zé)任鏈:

Transmitters.swift

......
class Transmitter {
    var nextLink:Transmitter?
    
    required init() {}
    
    func sendMessage(message:Message) {
        if (nextLink != nil) {
            nextLink!.sendMessage(message);
        } else {
            print("End of chain reached. Message not sent");
        }
    }
    
    private class func matchEmailSuffix(message:Message) -> Bool {
        if let index = message.from.characters.indexOf("@") {
            return message.to.hasSuffix(message.from[Range<String.Index>(start:
                index, end: message.from.endIndex)]);
        }
        return false
    }
    
    class func createChain() -> Transmitter? {
        let transmitterClasses:[Transmitter.Type] = [
            PriorityTransmitter.self,
            LocalTransmitter.self,
            RemoteTransmitter.self
        ]
        
        var link:Transmitter?
        
        for tClass in transmitterClasses.reverse() {
            let existingLink = link
            link = tClass.init()
            link?.nextLink = existingLink
        }
        return link
    }
}
......

應(yīng)用責(zé)任鏈模式

main.swift

let messages = [
    Message(from: "bob@example.com", to: "joe@example.com",
    subject: "Free for lunch?"),
    Message(from: "joe@example.com", to: "alice@acme.com",
    subject: "New Contracts"),
    Message(from: "pete@example.com", to: "all@example.com",
    subject: "Priority: All-Hands Meeting"),
]

if let chain = Transmitter.createChain() {
        for msg in messages {
            chain.sendMessage(msg)
        }
}

運(yùn)行程序:

Message to joe@example.com sent locally
Message to alice@acme.com sent remotely
Message to all@example.com sent as priority

責(zé)任鏈模式的變形

這里有幾種比較常用的變形潜叛,我們一一介紹。

應(yīng)用工廠方法模式

我們可以將責(zé)任鏈模式工廠方法模式或者抽象工廠方法模式結(jié)合起來壶硅,可以滿足不同的請(qǐng)求威兜。

Transmitters.swift

.....
    class func createChain(localOnly:Bool) -> Transmitter? {
        let transmitterClasses:[Transmitter.Type] = localOnly ? [PriorityTransmitter.self, LocalTransmitter.self]
        : [PriorityTransmitter.self, LocalTransmitter.self, RemoteTransmitter.self]
        
        var link:Transmitter?
        
        for tClass in transmitterClasses.reverse() {
            let existingLink = link
            link = tClass.init()
            link?.nextLink = existingLink
        }
        return link
    }
......

可以看出我們?cè)黾恿艘粋€(gè)只有本地發(fā)送的鏈。

main.swift

let messages = [
    Message(from: "bob@example.com", to: "joe@example.com",
    subject: "Free for lunch?"),
    Message(from: "joe@example.com", to: "alice@acme.com",
    subject: "New Contracts"),
    Message(from: "pete@example.com", to: "all@example.com",
    subject: "Priority: All-Hands Meeting"),
]

if let chain = Transmitter.createChain(true) {
        for msg in messages {
            chain.sendMessage(msg)
        }
}

運(yùn)行程序,可以看出遠(yuǎn)程發(fā)送的情況有所不同:

Message to joe@example.com sent locally
End of chain reached. Message not sent
Message to all@example.com sent as priority

表面請(qǐng)求是否被接受

現(xiàn)在森瘪,請(qǐng)求組件并不清楚請(qǐng)求是否被接受牡属,我們將對(duì)此做修改。

Transmitters.swift

class Transmitter {
    var nextLink:Transmitter?
    
    required init() {}
    
    func sendMessage(message:Message) -> Bool{
        if (nextLink != nil) {
           return nextLink!.sendMessage(message)
        
        } else {
            print("End of chain reached. Message not sent")
            return false
        }
    }
    
    private class func matchEmailSuffix(message:Message) -> Bool {
        if let index = message.from.characters.indexOf("@") {
            return message.to.hasSuffix(message.from[Range<String.Index>(start:
                index, end: message.from.endIndex)])
        }
        return false
    }
    
    class func createChain(localOnly:Bool) -> Transmitter? {
        let transmitterClasses:[Transmitter.Type] = localOnly ? [PriorityTransmitter.self, LocalTransmitter.self]
        : [PriorityTransmitter.self, LocalTransmitter.self, RemoteTransmitter.self]
        
        var link:Transmitter?
        
        for tClass in transmitterClasses.reverse() {
            let existingLink = link
            link = tClass.init()
            link?.nextLink = existingLink
        }
        return link
    }
}

class LocalTransmitter : Transmitter {
    override func sendMessage(message: Message) ->Bool{
        if (Transmitter.matchEmailSuffix(message)) {
            print("Message to \(message.to) sent locally")
            return true
        } else {
            return super.sendMessage(message)
        }
    }
}

class RemoteTransmitter : Transmitter {
    override func sendMessage(message: Message) ->Bool{
        if (!Transmitter.matchEmailSuffix(message)) {
            print("Message to \(message.to) sent remotely")
            return true
        } else {
            return super.sendMessage(message)
        }
    }
}

class PriorityTransmitter : Transmitter {
    override func sendMessage(message: Message) ->Bool{
        if (message.subject.hasPrefix("Priority")) {
            print("Message to \(message.to) sent as priority")
            return true
        } else {
            return super.sendMessage(message)
        }
    }
}

接下來修改main.swift:

let messages = [
    Message(from: "bob@example.com", to: "joe@example.com",
    subject: "Free for lunch?"),
    Message(from: "joe@example.com", to: "alice@acme.com",
    subject: "New Contracts"),
    Message(from: "pete@example.com", to: "all@example.com",
    subject: "Priority: All-Hands Meeting"),
]

if let chain = Transmitter.createChain(true) {
    for msg in messages {
        let handled = chain.sendMessage(msg)
        print("Message sent: \(handled)")
    }
}

運(yùn)行程序:

Message to joe@example.com sent locally
Message sent: true
End of chain reached. Message not sent
Message sent: false
Message to all@example.com sent as priority
Message sent: true

通知鏈中所有的環(huán)

在標(biāo)準(zhǔn)的責(zé)任鏈模式中扼睬,責(zé)任環(huán)前面所有的環(huán)都被請(qǐng)求過逮栅,責(zé)任環(huán)后面的環(huán)則不會(huì)被請(qǐng)求。這里我們將請(qǐng)求所有的環(huán)窗宇,無論責(zé)任環(huán)是否已經(jīng)被執(zhí)行措伐。

Transmitters.swift

class Transmitter {
    var nextLink:Transmitter?
    
    required init() {}
    
    func sendMessage(message:Message,handled: Bool = false) -> Bool{
        if (nextLink != nil) {
           return nextLink!.sendMessage(message,handled: handled)
        
        } else if(!handled) {
            print("End of chain reached. Message not sent")
        }
        return handled
    }
    
    private class func matchEmailSuffix(message:Message) -> Bool {
        if let index = message.from.characters.indexOf("@") {
            return message.to.hasSuffix(message.from[Range<String.Index>(start:
                index, end: message.from.endIndex)])
        }
        return false
    }
    
    class func createChain(localOnly:Bool) -> Transmitter? {
        let transmitterClasses:[Transmitter.Type] = localOnly ? [PriorityTransmitter.self, LocalTransmitter.self]
        : [PriorityTransmitter.self, LocalTransmitter.self, RemoteTransmitter.self]
        
        var link:Transmitter?
        
        for tClass in transmitterClasses.reverse() {
            let existingLink = link
            link = tClass.init()
            link?.nextLink = existingLink
        }
        return link
    }
}

class LocalTransmitter : Transmitter {
    override func sendMessage(message: Message, var handled:Bool) ->Bool{
        if (!handled && Transmitter.matchEmailSuffix(message)) {
            print("Message to \(message.to) sent locally")
            handled = true
        }
        return super.sendMessage(message, handled: handled)
    }
}

class RemoteTransmitter : Transmitter {
    override func sendMessage(message: Message, var handled:Bool) ->Bool{
        if (!handled && !Transmitter.matchEmailSuffix(message)) {
            print("Message to \(message.to) sent remotely")
            handled = true
        }
        return super.sendMessage(message, handled: handled)
    }
}

class PriorityTransmitter : Transmitter {
    var totalMessages = 0
    var handledMessages = 0
    
    override func sendMessage(message: Message, var handled:Bool) -> Bool {
        totalMessages++
        if (!handled && message.subject.hasPrefix("Priority")) {
            handledMessages++
            print("Message to \(message.to) sent as priority")
            print("Stats: Handled \(handledMessages) of \(totalMessages)")
            handled = true
        }
        return super.sendMessage(message, handled: handled)
    }
}

運(yùn)行程序:

Message to joe@example.com sent locallyMessage 
sent: trueEnd of chain reached. 
Message not sentMessage 
sent: false
Message to all@example.com sent as priority
Stats: Handled 1 of 3
Message sent: true
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市军俊,隨后出現(xiàn)的幾起案子侥加,更是在濱河造成了極大的恐慌,老刑警劉巖粪躬,帶你破解...
    沈念sama閱讀 210,978評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件担败,死亡現(xiàn)場(chǎng)離奇詭異昔穴,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)提前,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,954評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門吗货,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人狈网,你說我怎么就攤上這事宙搬。” “怎么了拓哺?”我有些...
    開封第一講書人閱讀 156,623評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵勇垛,是天一觀的道長。 經(jīng)常有香客問我士鸥,道長闲孤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,324評(píng)論 1 282
  • 正文 為了忘掉前任烤礁,我火速辦了婚禮崭放,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鸽凶。我一直安慰自己,他們只是感情好建峭,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,390評(píng)論 5 384
  • 文/花漫 我一把揭開白布玻侥。 她就那樣靜靜地躺著,像睡著了一般亿蒸。 火紅的嫁衣襯著肌膚如雪凑兰。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,741評(píng)論 1 289
  • 那天边锁,我揣著相機(jī)與錄音姑食,去河邊找鬼。 笑死茅坛,一個(gè)胖子當(dāng)著我的面吹牛音半,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播贡蓖,決...
    沈念sama閱讀 38,892評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼曹鸠,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了斥铺?” 一聲冷哼從身側(cè)響起彻桃,我...
    開封第一講書人閱讀 37,655評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎晾蜘,沒想到半個(gè)月后邻眷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體眠屎,經(jīng)...
    沈念sama閱讀 44,104評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評(píng)論 2 325
  • 正文 我和宋清朗相戀三年肆饶,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了改衩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,569評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡抖拴,死狀恐怖燎字,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情阿宅,我是刑警寧澤候衍,帶...
    沈念sama閱讀 34,254評(píng)論 4 328
  • 正文 年R本政府宣布,位于F島的核電站洒放,受9級(jí)特大地震影響蛉鹿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜往湿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,834評(píng)論 3 312
  • 文/蒙蒙 一妖异、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧领追,春花似錦他膳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,725評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至些膨,卻和暖如春蟀俊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背订雾。 一陣腳步聲響...
    開封第一講書人閱讀 31,950評(píng)論 1 264
  • 我被黑心中介騙來泰國打工肢预, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人洼哎。 一個(gè)月前我還...
    沈念sama閱讀 46,260評(píng)論 2 360
  • 正文 我出身青樓烫映,卻偏偏與公主長得像,于是被迫代替她去往敵國和親谱净。 傳聞我的和親對(duì)象是個(gè)殘疾皇子窑邦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,446評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容

  • 1 場(chǎng)景問題# 1.1 申請(qǐng)聚餐費(fèi)用## 來考慮這樣一個(gè)功能:申請(qǐng)聚餐費(fèi)用的管理。 很多公司都有這樣的福利壕探,就是項(xiàng)...
    七寸知架構(gòu)閱讀 3,118評(píng)論 3 58
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理冈钦,服務(wù)發(fā)現(xiàn),斷路器李请,智...
    卡卡羅2017閱讀 134,628評(píng)論 18 139
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫瞧筛、插件厉熟、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,058評(píng)論 4 62
  • 2015.4.13 陽光明媚 體育課 在破舊的籃球場(chǎng)前老師說要和旁邊男生班一起學(xué)跳舞 瞬間隊(duì)伍里炸開了鍋 大家都在...
    這是一個(gè)小號(hào)y閱讀 212評(píng)論 5 0
  • 初遇你時(shí) 正值晨曦 溫文而又特別 刻在心海中 多少個(gè)年年月月 絲毫未沖淡你的印記 我總是百般無奈 為何越發(fā)撩人心玄...
    明空mk閱讀 302評(píng)論 0 3