設(shè)計(jì)模式-責(zé)任鏈模式(十四)

  • 責(zé)任鏈模式
    每一個(gè)對(duì)象都有其下家的引用,這樣就可以形成一條鏈
    請(qǐng)求在鏈上傳遞,客戶端不知道哪一個(gè)對(duì)象會(huì)處理這個(gè)請(qǐng)求,這就可以讓客戶端在不知道的情況下重新分配責(zé)任.

在處理HTTP 請(qǐng)求的框架里面可以用該模式.
實(shí)例:擊鼓傳花

責(zé)任鏈模式可以一個(gè)對(duì)象只處理本人關(guān)注的,省得寫一堆 if else if,我們只寫一個(gè)if else
形如:

if (我想處理) {
  // 處理之 
  this.execute();
}else{
    //扔給下一個(gè)
    successor.handle();
}
  • 上類圖:
image.png
  • 示例代碼:
  1. 先定義一個(gè)鏈接口,里面要有個(gè) handleChain 處理鏈的方法
package com.byedbl.chain;

/**
 * The interface of the chain
 * You can use AddChain function to modify the chain dynamically
 */
public interface Chain {
    void addChain(Chain c);

    void handleChain(String mesg);

    Chain getChain();
}
  1. 定義一堆實(shí)現(xiàn)者,這里以經(jīng)典的代碼管理為例,有manager,PM,Programmer,QA,Others角色
package com.byedbl.chain;
/**
 *  A beginner of the chain
 *  The resposibility of manager is to get a project
 */
public class Manager implements Chain {
    private Chain nextChain = null;
    private String responsibility = "Get Project";
    ;

    public Manager() {
    }

    public void addChain(Chain c) {
        nextChain = c;
    }

    public Chain getChain() {
        return nextChain;
    }

    public void handleChain(String mesg) {
        if (mesg.equals(responsibility)) {
            System.out.println("A manager  -->  Get a Project");
        } else {
            if (nextChain != null) {
                nextChain.handleChain(mesg);
            }
        }
    }

}
package com.byedbl.chain;
/**
 *  A member of the chain
 *  The resposibility of PM is to design the project
 */
public class ProjectManager implements Chain {
    private Chain nextChain = null;
    private String responsibility = "Design";

    public ProjectManager() {
    }

    public void addChain(Chain c) {
        nextChain = c;
    }

    public Chain getChain() {
        return nextChain;
    }

    public void handleChain(String mesg) {
        if (mesg.equals(responsibility)) {
            System.out.println("A PM  -->  Design");
        } else {
            if (nextChain != null) {
                nextChain.handleChain(mesg);
            }
        }
    }

}
package com.byedbl.chain;
/**
 *  A member of the chain
 *  The resposibility of Programmer is coding
 */
public class Programmer implements Chain {
    private Chain nextChain = null;
    private String responsibility = "Coding";

    public Programmer() {
    }

    public void addChain(Chain c) {
        nextChain = c;
    }

    public Chain getChain() {
        return nextChain;
    }

    public void handleChain(String mesg) {
        if (mesg.equals(responsibility)) {
            System.out.println("A Programmer  -->  Coding");
        } else {
            if (nextChain != null) {
                nextChain.handleChain(mesg);
            }
        }
    }

}

package com.byedbl.chain;
/**
 *  A member of the chain
 *  The resposibility of QA is test
 */
public class QA implements Chain {
    private Chain nextChain = null;
    private String responsibility = "Test";

    public QA() {
    }

    public void addChain(Chain c) {
        nextChain = c;
    }

    public Chain getChain() {
        return nextChain;
    }

    public void handleChain(String mesg) {
        if (mesg.equals(responsibility)) {
            System.out.println("A QA  -->  Test");
        } else {
            if (nextChain != null) {
                nextChain.handleChain(mesg);
            }
        }
    }

}
package com.byedbl.chain;
/**
 *  The end of the chain
 *  The resposibility of Others is handle exeception 
 */
public class Others implements Chain {
    private Chain nextChain = null;
    private String responsibility = "";
    
    public Others() {
    }
    public void addChain(Chain c) {
        nextChain = c;
    }
    
    public Chain getChain() {
        return nextChain;
    }
    
    public void handleChain(String mesg) {
            System.out.println("No one can handle -->  " + mesg);
    }
    
}
  1. 客戶端代碼
package com.byedbl.chain;
/**
 *  A client to test
 */
public class Test  {
    public static void main(String[] args) {
        Manager aManager = new Manager();
        ProjectManager aPM = new ProjectManager();
        Programmer aProgrammer = new Programmer();
        QA aQA = new QA();
        Others others = new Others();

        aManager.addChain(aPM);
        aPM.addChain(aProgrammer);
        aProgrammer.addChain(aQA);
        aQA.addChain(others);

        aManager.handleChain("Get Project");
        aManager.handleChain("Design");
        aManager.handleChain("Coding");
        aManager.handleChain("Test");
        aManager.handleChain("Kill La Deng !");
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末昼牛,一起剝皮案震驚了整個(gè)濱河市葵陵,隨后出現(xiàn)的幾起案子溯饵,更是在濱河造成了極大的恐慌赂蕴,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件朝巫,死亡現(xiàn)場(chǎng)離奇詭異坠陈,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)剩岳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門贞滨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人卢肃,你說(shuō)我怎么就攤上這事疲迂。” “怎么了莫湘?”我有些...
    開封第一講書人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵尤蒿,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我幅垮,道長(zhǎng)腰池,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任忙芒,我火速辦了婚禮示弓,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘呵萨。我一直安慰自己奏属,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開白布潮峦。 她就那樣靜靜地躺著囱皿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪忱嘹。 梳的紋絲不亂的頭發(fā)上嘱腥,一...
    開封第一講書人閱讀 51,754評(píng)論 1 307
  • 那天,我揣著相機(jī)與錄音拘悦,去河邊找鬼齿兔。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的分苇。 我是一名探鬼主播添诉,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼组砚!你這毒婦竟也來(lái)了吻商?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤糟红,失蹤者是張志新(化名)和其女友劉穎艾帐,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盆偿,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡柒爸,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了事扭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片捎稚。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖求橄,靈堂內(nèi)的尸體忽然破棺而出今野,到底是詐尸還是另有隱情,我是刑警寧澤罐农,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布条霜,位于F島的核電站,受9級(jí)特大地震影響涵亏,放射性物質(zhì)發(fā)生泄漏宰睡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一气筋、第九天 我趴在偏房一處隱蔽的房頂上張望拆内。 院中可真熱鬧,春花似錦宠默、人聲如沸麸恍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)抹沪。三九已至,卻和暖如春艾君,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背肄方。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工冰垄, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓虹茶,卻偏偏與公主長(zhǎng)得像逝薪,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蝴罪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355

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

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理董济,服務(wù)發(fā)現(xiàn),斷路器要门,智...
    卡卡羅2017閱讀 134,672評(píng)論 18 139
  • 工廠模式類似于現(xiàn)實(shí)生活中的工廠可以產(chǎn)生大量相似的商品虏肾,去做同樣的事情,實(shí)現(xiàn)同樣的效果;這時(shí)候需要使用工廠模式欢搜。簡(jiǎn)單...
    舟漁行舟閱讀 7,771評(píng)論 2 17
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法封豪,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法炒瘟,繼承相關(guān)的語(yǔ)法吹埠,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 31,644評(píng)論 18 399
  • 1 場(chǎng)景問(wèn)題# 1.1 申請(qǐng)聚餐費(fèi)用## 來(lái)考慮這樣一個(gè)功能:申請(qǐng)聚餐費(fèi)用的管理疮装。 很多公司都有這樣的福利缘琅,就是項(xiàng)...
    七寸知架構(gòu)閱讀 3,138評(píng)論 3 58
  • 今天是力輝和皎皎正式分居第五十天,也是皎皎懷孕第八個(gè)月廓推。這五十天對(duì)皎皎來(lái)說(shuō)是結(jié)婚以來(lái)過(guò)得最舒心最愉快的五十天刷袍,簡(jiǎn)直...
    小飛鹿1982閱讀 465評(píng)論 8 5