一、以加薪代码为例
//加薪代码初步
class Request { // 申请类
private String requestType;// 申请类别
private String RequestContent;// 申请内容
private int number;// 数量
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestContent() {
return RequestContent;
}
public void setRequestContent(String requestContent) {
RequestContent = requestContent;
}
}
class Manager {
protected String name;
public Manager(String name) {
super();
this.name = name;
}
public void GetResult(String managerLevel, Request request) {
if (managerLevel == "经理") {
if (request.getRequestType() == "请假" && request.getNumber() <= 2) {
System.out.println(name + request.getRequestContent() + "数量" + request.getNumber() + "被批准");
} else {
System.out.println(name + request.getRequestContent() + "数量" + request.getNumber() + "我无权批准");
}
} else if (managerLevel == "总监") {
if (request.getRequestType() == "请假" && request.getNumber() <= 5) {
System.out.println(name + request.getRequestContent() + "数量" + request.getNumber() + "被批准");
} else {
System.out.println(name + request.getRequestContent() + "数量" + request.getNumber() + "我无权批准");
}
} else if (managerLevel == "总经理") {
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 if (request.getRequestType() == "加薪" && request.getNumber() > 500) {
System.out.println(name + request.getRequestContent() + "数量" + request.getNumber() + "再说吧");
}
}
}
}
public class Chains {
public static void main(String[] args) {
Manager jinli = new Manager("经理");
Manager zongjian = new Manager("总监");
Manager zongjingli = new Manager("总经理");
Request request = new Request();
request.setRequestType("加薪");
request.setRequestContent("小菜请求加薪");
request.setNumber(1000);
jinli.GetResult("经理", request);
zongjian.GetResult("总监", request);
zongjingli.GetResult("总经理", request);
Request request2 = new Request();
request.setRequestType("请假");
request.setRequestContent("小菜请假");
request.setNumber(1000);
jinli.GetResult("经理", request2);
zongjian.GetResult("总监", request2);
zongjingli.GetResult("总经理", request2);
}
}
二、职责链模式
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
abstract class Handler {
protected Handler successor;
public void SetSuccessor(Handler successor) {
this.successor = successor;
}
public abstract void HandleRequest(int request);
}
class ConcreteHandler1 extends Handler {
public void HandleRequest(int request) {
if (request >= 0 && request < 10) {
System.out.println("处理请求 " + this.getClass().getSimpleName() + " " + request);
} else if (successor != null) {
successor.HandleRequest(request);
}
}
}
class ConcreteHandler2 extends Handler {
public void HandleRequest(int request) {
if (request >= 10 && request < 20) {
System.out.println("处理请求" + this.getClass().getSimpleName() + " " + request);
} else if (successor != null) {
successor.HandleRequest(request);
}
}
}
class ConcreteHandler3 extends Handler {
public void HandleRequest(int request) {
if (request >= 20 && request < 30) {
System.out.println("处理请求" + this.getClass().getSimpleName() + " " + request);
} else if (successor != null) {
successor.HandleRequest(request);
}
}
}
public class Chains {
public static void main(String[] args) {
// TODO Auto-generated method stub
Handler h1 = new ConcreteHandler1();
Handler h2 = new ConcreteHandler2();
Handler h3 = new ConcreteHandler3();
h1.SetSuccessor(h2);
h2.SetSuccessor(h3);
int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };
for (int request : requests) {
h1.HandleRequest(request);
}
}
}
三、加薪代码重构
class Request { // 申请类
private String requestType;// 申请类别
private String RequestContent;// 申请内容
private int number;// 数量
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestContent() {
return RequestContent;
}
public void setRequestContent(String requestContent) {
RequestContent = requestContent;
}
}
abstract class Manager { // 管理者类
protected String name;
protected Manager superior;// 管理者的上级
public Manager(String name) {
super();
this.name = name;
}
// 设置管理者的上级
public void setSuperior(Manager superior) {
this.superior = superior;
}
// 申请请求
abstract public void RequestApplications(Request request);
}
class CommonManager extends Manager {// 经理类
public CommonManager(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@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);
// TODO Auto-generated constructor stub
}
@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);
// TODO Auto-generated constructor stub
}
@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 if (request.getRequestType() == "加薪" && request.getNumber() > 500) {
System.out.println(name + request.getRequestContent() + "数量" + request.getNumber() + "再说吧");
}
}
}
public class Chains {
public static void main(String[] args) {
Manager jinli = new CommonManager("经理");
Manager zongjian = new Majordomo("总监");
Manager zongjingli = new GeneralManager("总经理");
jinli.setSuperior(zongjian);
zongjian.setSuperior(zongjingli);
Request request = new Request();
request.setRequestType("请假");
request.setRequestContent("小菜请假");
request.setNumber(1);
jinli.RequestApplications(request);
Request request2 = new Request();
request.setRequestType("请假");
request.setRequestContent("小菜请假");
request.setNumber(4);
jinli.RequestApplications(request2);
Request request3 = new Request();
request.setRequestType("加薪");
request.setRequestContent("小菜请求加薪");
request.setNumber(500);
jinli.RequestApplications(request3);
Request request4 = new Request();
request.setRequestType("加薪");
request.setRequestContent("小菜请求加薪");
request.setNumber(1000);
jinli.RequestApplications(request4);
}
}