学习笔记
职责链模式的结构 和装饰模式的结构是一样的。
请求类型
- public class Request {
- private String name;
- private String requestType;
- private int number;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getRequestType() {
- return requestType;
- }
- public void setRequestType(String requestType) {
- this.requestType = requestType;
- }
- public int getNumber() {
- return number;
- }
- public void setNumber(int number) {
- this.number = number;
- }
- }
public class Request {
private String name;
private String requestType;
private int number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
管理的抽象类
- public abstract class Manager {
- protected String name;
- //管理者的上级
- protected Manager superior;
- public Manager(String name) {
- this.name=name;
- }
- //设置管理者的上级
- public void SetSuperior(Manager superior){
- this.superior=superior;
- }
- //声请请求
- abstract public void requestApplications(Request requset);
- public static void main(String[]args){
- CommonManager commonManager =new CommonManager("hyy");
- GeneralManager generalManager =new GeneralManager("ysen");
- Majordomo majordomo =new Majordomo("hdx");
- //设置上下级
- commonManager.SetSuperior(majordomo);
- majordomo.SetSuperior(generalManager);
- Request request = new Request();
- request.setName("ysen");
- request.setRequestType("加薪");
- request.setNumber(800);
- commonManager.requestApplications(request);
- }
- }
public abstract class Manager {
protected String name;
//管理者的上级
protected Manager superior;
public Manager(String name) {
this.name=name;
}
//设置管理者的上级
public void SetSuperior(Manager superior){
this.superior=superior;
}
//声请请求
abstract public void requestApplications(Request requset);
public static void main(String[]args){
CommonManager commonManager =new CommonManager("hyy");
GeneralManager generalManager =new GeneralManager("ysen");
Majordomo majordomo =new Majordomo("hdx");
//设置上下级
commonManager.SetSuperior(majordomo);
majordomo.SetSuperior(generalManager);
Request request = new Request();
request.setName("ysen");
request.setRequestType("加薪");
request.setNumber(800);
commonManager.requestApplications(request);
}
}
管理的具体类
- //总经理
- public class GeneralManager extends Manager {
- public GeneralManager(String name) {
- super(name);
- }
- @Override
- public void requestApplications(Request request) {
- if(request.getRequestType().equals("请假")&&request.getNumber()<=7){
- System.out.println(request.getName()+request.getRequestType()+request.getNumber()+"被批准" );
- }else if(request.getRequestType().equals("加薪")&&request.getNumber()<=800){
- System.out.println(request.getName()+request.getRequestType()+request.getNumber()+"被批准" );
- }
- else{
- if(this.superior!=null){
- this.superior.requestApplications(request);
- }
- }
- }
- }
- //总监
- public class Majordomo extends Manager {
- public Majordomo(String name) {
- super(name);
- }
- @Override
- public void requestApplications(Request request) {
- if(request.getRequestType().equals("请假")&& request.getNumber() <=5){
- System.out.println(request.getName()+request.getRequestType()+request.getNumber()+"被批准" );
- }else{
- if(this.superior!=null){
- this.superior.requestApplications(request);
- }
- }
- }
- }
- //经理
- public class CommonManager extends Manager {
- public CommonManager(String name) {
- super(name);
- }
- @Override
- public void requestApplications(Request request) {
- if(request.getRequestType().equals("请假")&& request.getNumber()<=2){
- System.out.println(request.getName()+request.getRequestType()+request.getNumber()+"被批准" );
- }else{
- if(this.superior!=null){
- this.superior.requestApplications(request);
- }
- }
- }
- }