定义:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,将这些对象连成一条链,并沿这条链传递请求,直到有一个对象处理它为止。
例题:加薪申请、上报与审批
员工向经理发起加薪申请,经理无权决定,需要向总监汇报,如果加薪额度超过总监权力范围,需要向总经理汇报。
员工还可以提交请假申请,经理可以决定2天以下的假,总监可以决定5天以下的假,其余都要上报总经理。
加薪代码初步
//申请:小菜请求加薪2000/小菜请假3天
class Request{
private String requestType;//申请类别
private String requestContent;//申请内容
private int number;//数量
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestContent() {
return requestContent;
}
public void setRequestContent(String requestContent) {
this.requestContent = requestContent;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
//管理者
class Manager{
protected String name;
public Manager(String name) {
this.name=name;
}
public void GetResult(String managerLevel,Request request) {
System.out.println(request.getRequestType()+":"+request.getNumber());
if(managerLevel=="经理") {
if(request.getRequestType()=="请假"&&request.getNumber()<=2) {
System.out.println("数量<=2,被批准");
}else {
System.out.println("数量>2,无权批准");
}
}else if(managerLevel=="总监") {
if(request.getRequestType()=="请假"&&request.getNumber()<=5) {
System.out.println("数量<=5,被批准");
}else {
System.out.println("数量>5,无权批准");
}
}else if(managerLevel=="总经理") {
if(request.getRequestType()=="请假") {
System.out.println("请假,被批准");
}else if(request.getRequestType()=="加薪"&&request.getNumber()<=500) {
System.out.println("加薪<=500,被批准");
}else if(request.getRequestType()=="加薪"&&request.getNumber()>500) {
System.out.println("加薪>500,无权批准");
}
}
}
}
public class Main{
public static void main(String[] args){
Manager jinli=new Manager("锦鲤");
Manager zongjian=new Manager("纵剪");
Manager zongjingli=new Manager("宗经理");
Request request=new Request();//小菜请求加薪1000
request.setRequestType("加薪");
request.setRequestContent("小菜请求加薪");
request.setNumber(1000);
jinli.GetResult("经理", request);
zongjian.GetResult("总监", request);
zongjingli.GetResult("总经理", request);
System.out.println("=======");
Request request2=new Request();//小菜请假3天
request2.setRequestType("请假");
request2.setRequestContent("小菜请假");
request2.setNumber(3);
jinli.GetResult("经理", request2);
zongjian.GetResult("总监", request2);
zongjingli.GetResult("总经理", request2);
}
}
出现问题:Manager类的GetResult方法比较长,且有太多的分支判断,这是不好的设计,因为可能还会增加其他的管理美别,比如项目经理、部门经理、人力总监、副总经理等等,那就意味着需要去更改Manager类,这个类承担了太多的责任,违背了单一职责原则,增加新的管理类别,需要修改这个类,违背了开放-封闭原则。
重构加薪代码
class Request{
private String requestType;//申请类别
private String requestContent;//申请内容
private int number;//数量
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestContent() {
return requestContent;
}
public void setRequestContent(String requestContent) {
this.requestContent = requestContent;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
//管理者类
abstract class Manager{
protected String name;
//管理者的上级
protected Manager superior;
public Manager(String name) {
this.name=name;
}
//设置管理者的上级
public void SetSuperrior(Manager superior) {
this.superior=superior;
}
abstract public void RequestApplications(Request request);
}
class CommonManager extends Manager{//经理类
public CommonManager(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
// TODO Auto-generated method stub
if(request.getRequestType()=="请假"&&request.getNumber()<=2) {
System.out.println(name+":"+request.getRequestContent()+"数量"+request.getNumber()+"被批准");
}else {//其余的申请都需转到上级
if(superior!=null) {
superior.RequestApplications(request);
}
}
}
}
class Majordomo extends Manager{//总监类
public Majordomo(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
// TODO Auto-generated method stub
if(request.getRequestType()=="请假"&&request.getNumber()<=5) {
System.out.println(name+":"+request.getRequestContent()+"数量"+request.getNumber()+"被批准");
}else {
if(superior!=null) {
superior.RequestApplications(request);
}
}
}
}
class GeneralManager extends Manager{
public GeneralManager(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
// TODO Auto-generated method stub
if(request.getRequestType()=="请假") {
System.out.println(name+":"+request.getRequestContent()+"数量"+request.getNumber()+"被批准");
}else if(request.getRequestType()=="加薪"&&request.getNumber()<=500) {
System.out.println(name+":"+request.getRequestContent()+"数量"+request.getNumber()+"被批准");
}else {
System.out.println(name+":"+request.getRequestContent()+"数量"+request.getNumber()+"再说吧");
}
}
}
public class Main{
public static void main(String[] args){
CommonManager jinli=new CommonManager("锦鲤");
Majordomo zongjian=new Majordomo("纵剪");
GeneralManager zhongjingli=new GeneralManager("宗经理");
jinli.SetSuperrior(zongjian);
zongjian.SetSuperrior(zhongjingli);
Request request=new Request();
request.setRequestType("请假");
request.setRequestContent("小菜请假");
request.setNumber(1);
jinli.RequestApplications(request);
Request request2=new Request();
request2.setRequestType("请假");
request2.setRequestContent("小菜请假");
request2.setNumber(4);
jinli.RequestApplications(request2);
Request request3=new Request();
request3.setRequestType("加薪");
request3.setRequestContent("小菜加薪");
request3.setNumber(1000);
jinli.RequestApplications(request3);
}
}
结果为:
锦鲤:小菜请假数量1被批准
纵剪:小菜请假数量4被批准
宗经理:小菜加薪数量1000再说吧
Handler类:定义一个处理请示的接口。
ConcreteHandler类:具体处理者类,处理它所负责的请求,可访问它的后继者,如果可处理该请求,就处理之,否则就将该请求转发给它的后继者。
ConcretelHandler1类:当请求数在0到10之间则有权处理,否则转到下一位。
ConcreteHandler2类:当请求数在10到20之间则有权处理,否则转到下一位。
ConcreteHandler3类:当请求数在20到30之间则有权处理,否则转到下一位。
abstract class Handler{
protected String name;
protected Handler successor;//设置继任者
public void SetSuccessor(Handler successor) {
this.successor=successor;
}
public abstract void HandleRequest(int request);
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class ConcreteHandler1 extends Handler{
@Override
public void HandleRequest(int request) {
// TODO Auto-generated method stub
if(request>=0&&request<10) {
System.out.println(this.getName()+"处理请求"+request);
}else if(successor!=null) {
successor.HandleRequest(request);
}
}
}
class ConcreteHandler2 extends Handler{
@Override
public void HandleRequest(int request) {
// TODO Auto-generated method stub
if(request>=10&&request<20) {
System.out.println(this.getName()+"处理请求"+request);
}else if(successor!=null) {
successor.HandleRequest(request);
}
}
}
class ConcreteHandler3 extends Handler{
@Override
public void HandleRequest(int request) {
// TODO Auto-generated method stub
if(request>=20&&request<30) {
System.out.println(this.getName()+"处理请求"+request);
}else if(successor!=null) {
successor.HandleRequest(request);
}
}
}
public class Main{
public static void main(String[] args){
Handler h1=new ConcreteHandler1();
h1.setName("审批人1");
Handler h2=new ConcreteHandler2();
h2.setName("审批人2");
Handler h3=new ConcreteHandler3();
h3.setName("审批人3");
h1.SetSuccessor(h2);
h2.SetSuccessor(h3);
int[]requests= {2,5,14,22,18,3,27,20};
for(int i=0;i<requests.length;i++) {
h1.HandleRequest(requests[i]);
}
}
}
结果为:
审批人1处理请求2
审批人1处理请求5
审批人2处理请求14
审批人3处理请求22
审批人2处理请求18
审批人1处理请求3
审批人3处理请求27
审批人3处理请求20
优点:
1、请求者和接收者松散耦合
2、动态组合职责
缺点:
1、产生很多细粒度对象
2、不一定能被处理