- 責(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
-
示例代碼:
- 先定義一個(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();
}
- 定義一堆實(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);
}
}
- 客戶端代碼
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 !");
}
}