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、具体实现类
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、测试+组装职责链
public classtest {
public static voidmain(String[] args) {
//组装职责链
ProjectManager projectManager =newProjectManager();
DeptManager deptManager =newDeptManager();
projectManager.setHandlerNext(deptManager);
String response = projectManager.handlerRequest("zhangsan",34);
}
}
网友评论