go責(zé)任鏈行為型設(shè)計(jì)模式Chain?Of?Responsibility
簡(jiǎn)介
使多個(gè)對(duì)象都有機(jī)會(huì)處理請(qǐng)求,從而避免請(qǐng)求的發(fā)送者和接收者之間的耦合關(guān)系。將這些對(duì)象連成一條鏈,并沿著這條鏈傳遞該請(qǐng)求,直到有一個(gè)對(duì)象處理它為止。
角色
Handler 接口
定義處理方法簽名,設(shè)置nextHandler方法
Concrete Handler 具體類
實(shí)現(xiàn)各自handler邏輯
- BaseHandler 封裝一層handler,可有可無
類圖
如圖,在 client 中,將 handler 一個(gè)個(gè)串起來,每個(gè) handler 處理完后可決定是否向后傳遞。
代碼
interface Handler { public function setNext(Handler $handler): Handler; public function handle(string $request): string; } abstract class AbstractHandler implements Handler { private $nextHandler; public function setNext(Handler $handler): Handler { $this->nextHandler = $handler; return $handler; } public function handle(string $request): string { if ($this->nextHandler) { return $this->nextHandler->handle($request); } return ""; } } class MonkeyHandler extends AbstractHandler { public function handle(string $request): string { if ($request === "Banana") { return "Monkey: I'll eat the " . $request . ".\n"; } else { return parent::handle($request); } } } class SquirrelHandler extends AbstractHandler { public function handle(string $request): string { if ($request === "Nut") { return "Squirrel: I'll eat the " . $request . ".\n"; } else { return parent::handle($request); } } } class DogHandler extends AbstractHandler { public function handle(string $request): string { if ($request === "MeatBall") { return "Dog: I'll eat the " . $request . ".\n"; } else { return parent::handle($request); } } } function clientCode(Handler $handler) { foreach (["Nut", "Banana", "Cup of coffee"] as $food) { echo "Client: Who wants a " . $food . "?\n"; $result = $handler->handle($food); if ($result) { echo " " . $result; } else { echo " " . $food . " was left untouched.\n"; } } } $monkey = new MonkeyHandler(); $squirrel = new SquirrelHandler(); $dog = new DogHandler(); $monkey->setNext($squirrel)->setNext($dog); echo "Chain: Monkey > Squirrel > Dog\n\n"; clientCode($monkey); echo "\nSubchain: Squirrel > Dog\n\n"; clientCode($squirrel);
output
Chain: Monkey > Squirrel > Dog
Client: Who wants a Nut?
Squirrel: I'll eat the Nut.
Client: Who wants a Banana?
Monkey: I'll eat the Banana.
Client: Who wants a Cup of coffee?
Cup of coffee was left untouched.
Subchain: Squirrel > Dog
Client: Who wants a Nut?
Squirrel: I'll eat the Nut.
Client: Who wants a Banana?
Banana was left untouched.
Client: Who wants a Cup of coffee?
Cup of coffee was left untouched.
以上就是go責(zé)任鏈行為型設(shè)計(jì)模式Chain Of Responsibility的詳細(xì)內(nèi)容,更多關(guān)于go責(zé)任鏈行為型設(shè)計(jì)模式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
golang復(fù)制文件夾移動(dòng)到另一個(gè)文件夾實(shí)現(xiàn)方法詳解
這篇文章主要為大家介紹了golang復(fù)制文件夾并移動(dòng)到另一個(gè)文件夾實(shí)現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Go 結(jié)構(gòu)體、數(shù)組、字典和 json 字符串的相互轉(zhuǎn)換方法
今天小編就為大家分享一篇Go 結(jié)構(gòu)體、數(shù)組、字典和 json 字符串的相互轉(zhuǎn)換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08Go語言基礎(chǔ)設(shè)計(jì)模式之策略模式示例詳解
這篇文章主要為大家介紹了Go語言基礎(chǔ)設(shè)計(jì)模式之策略模式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11Golang實(shí)現(xiàn)協(xié)程超時(shí)控制的方式總結(jié)
我們知道,go協(xié)程如果不做好處理,很容易造成內(nèi)存泄漏,所以對(duì)goroutine做超時(shí)控制,才能有效避免這種情況發(fā)生,本文為大家整理了兩個(gè)常見的Golang超時(shí)控制方法,需要的可以收藏一下2023-05-05Go如何實(shí)現(xiàn)HTTP請(qǐng)求限流示例
本篇文章主要介紹了Go如何實(shí)現(xiàn)HTTP請(qǐng)求限流示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04