標籤:注意 應用 職責鏈 pre node write 中介軟體 func rip
概念
職責鏈模式是使多個對象都有機會處理請求,從而避免請求的寄件者和接受者之間的耦合關係。將這個對象連成一條鏈,並沿著這條鏈傳遞該請求,直到有一個對象處理他為止。
鏈中收到請求的對象要麼親自處理它,要麼轉寄給下一個候選者。提交方並不明確有多少個對象會處理它,任一候選者都可以響應相應的請求,可以在運行時刻決定哪些候選者參與到鏈中。
作用
- dom的冒泡有些類似職責鏈
- nodejs當controller中有很多負責操作邏輯的時候拆分中介軟體
- 解耦寄件者和接收者
注意事項
JavaScript中的每一次“.”是有代價的,要在必要的時候應用
<script type="text/javascript"> function laoban(xiangmujingli){ if(xiangmujingli){ this.xiangmujingli = xiangmujingli; } } laoban.prototype.write = function (php) { this.xiangmujingli.write(php); } function xiangmujingli(coder) { if(coder){ this.coder=coder; }; } xiangmujingli.prototype.write=function (php) { this.coder.write(php); } function coder(php) { // this.write(php); } coder.prototype.write=function (php) { console.log("coding..." + php) } var begin = new laoban(new xiangmujingli(new coder())); begin.write("php");</script>
JavaScript---設計模式之職責鏈模式