1排作、抽象基類
public abstract classHandler {
protected Handler handlerNext;
public abstractString handlerRequest(String user,doublefee);
publicHandler getHandlerNext() {
return HandlerNext;
}
public void setHandlerNext(Handler handlerNext) {
HandlerNext= handlerNext;
}
}
2、具體實現(xiàn)類
1)public classProjectManagerextendsHandler {
@Override
publicString handlerRequest(String user,doublefee) {
String response =newString("project");
return this.getHandlerNext().handlerRequest("user", fee);
}
}
2)
public classDeptManagerextendsHandler {
@Override
publicString handlerRequest(String user,doublefee) {
String response =newString("Dept manager");
return response;
}
}
3村生、測試+組裝職責(zé)鏈
public classtest {
public static voidmain(String[] args) {
//組裝職責(zé)鏈
ProjectManager projectManager =newProjectManager();
DeptManager deptManager =newDeptManager();
projectManager.setHandlerNext(deptManager);
String response = projectManager.handlerRequest("zhangsan",34);
}
}