Table of contents
Traditional solution to OA system approval, traditional design solution (class diagram)
Analysis of traditional solutions to OA system approval problems
Basic introduction to the chain of responsibility model
Principle Class Diagram of Chain of Responsibility Pattern
Explanation of the principle class diagram
Responsibility chain mode solves OA system procurement approval
Application example requirements
Idea analysis and diagram (class diagram)
Notes and Details of the Chain of Responsibility Pattern
problem introduction
Purchasing approval items of the school OA system: the demand is that
the purchaser purchases teaching equipment
1) If the amount is less than or equal to 5000, it will be approved by the teaching director (0<=x<=5000)
2) If the amount is less than or equal to 10000, it will be approved by the dean (5000 <x<=10000)
3) If the amount is less than or equal to 30000, it will be approved by the vice principal (10000<x<=30000)
4) If the amount exceeds 30000, it will be approved by the principal (30000<x)
Please design a program to complete the purchase approval project
Traditional solution to OA system approval, traditional design solution (class diagram)
Analysis of traditional solutions to OA system approval problems
1) The traditional method is: after receiving a purchase request, call the corresponding Approver (approver) to complete the approval according to the purchase amount.
2) Problem analysis in the traditional way: the client will use branch judgment (such as switch) to process different procurement requests, so there are the following
problems (1) If the approval amount of personnel at each level changes, the client also Need to change (2) The client must clearly know how many approval levels and accesses there are
3) In this way, there is a strong coupling relationship between the processing of a purchase request and the Approver (approver), which is not conducive to code expansion and maintenance
Basic introduction to the chain of responsibility model
basic introduction
1) Chain of Responsibility Pattern (Chain of Responsibility Pattern), also known as Chain of Responsibility Pattern, creates a chain of receiver objects for requests. This pattern decouples the sender and receiver of a request.
2) The Chain of Responsibility pattern typically has each receiver contain a reference to another receiver. If an object cannot handle the request, it passes the same request to the next recipient, and so on.
3) This type of design pattern is a behavioral pattern
Principle Class Diagram of Chain of Responsibility Pattern
Explanation of the principle class diagram
1) Handler: An abstract handler, which defines an interface for processing requests, and also means another Handler
2) ConcreteHandlerA, B is a specific handler, which handles the request it is responsible for, and can access its successor (that is, the next processing If the current request can be processed, it will be processed, otherwise, the request will be handed over to a successor for processing, thus forming a chain of responsibility
3) Request, meaning many attributes, representing a request
Responsibility chain mode solves OA system procurement approval
Application example requirements
Write a program to complete the purchase approval project of the school OA system: Demand
Purchaser purchases teaching equipment
If the amount is less than or equal to 5000, it will be approved by the teaching director
If the amount is less than or equal to 10000, it will be approved by the dean
If the amount is less than or equal to 30000, it will be approved by the vice principal
If the amount exceeds More than 30,000, with the approval of the principal
Idea analysis and diagram (class diagram)
code landing
PurchaseRequest
public class PurchaseRequest {
private int type = 0; //请求类型
private float price = 0.0f; //请求金额
private int id = 0;
//构造器
public PurchaseRequest(int type, float price, int id) {
this.type = type;
this.price = price;
this.id = id;
}
public int getType() {
return type;
}
public float getPrice() {
return price;
}
public int getId() {
return id;
}
}
Approver
public abstract class Approver {
Approver approver;//下一个处理者
String name;//名字
public Approver(String name) {
this.name = name;
}
//下一个处理者
public void setApprover(Approver approver) {
this.approver = approver;
}
//处理审批请求的方法,得到一个请求, 处理是子类完成,因此该方法做成抽象
public abstract void processRequest(PurchaseRequest purchaseRequest);
}
ViceSchoolMasterApprover
public class ViceSchoolMasterApprover extends Approver{
public ViceSchoolMasterApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if(purchaseRequest.getPrice() < 10000 && purchaseRequest.getPrice() <= 30000) {
System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理");
}else {
approver.processRequest(purchaseRequest);
}
}
}
DepartmentApprover
public class DepartmentApprover extends Approver{
public DepartmentApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if(purchaseRequest.getPrice() <= 5000) {
System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理");
}else {
approver.processRequest(purchaseRequest);
}
}
}
CollegeApprover
public class CollegeApprover extends Approver{
public CollegeApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if(purchaseRequest.getPrice() < 5000 && purchaseRequest.getPrice() <= 10000) {
System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理");
}else {
approver.processRequest(purchaseRequest);
}
}
}
SchoolMasterApprover
public class SchoolMasterApprover extends Approver{
public SchoolMasterApprover(String name) {
super(name);
}
@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if(purchaseRequest.getPrice() > 30000) {
System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理");
}else {
approver.processRequest(purchaseRequest);
}
}
}
Client
public class Client {
public static void main(String[] args) {
PurchaseRequest purchaseRequest = new PurchaseRequest(1, 31000, 1);
//创建相关的审批人
DepartmentApprover departmentApprover = new DepartmentApprover("张主任");
CollegeApprover collegeApprover = new CollegeApprover("李院长");
ViceSchoolMasterApprover viceSchoolMasterApprover = new ViceSchoolMasterApprover("王副校");
SchoolMasterApprover schoolMasterApprover = new SchoolMasterApprover("佟校长");
//需要将各个审批级别的下一个设置好 (处理人构成环形: )
departmentApprover.setApprover(collegeApprover);
collegeApprover.setApprover(viceSchoolMasterApprover);
viceSchoolMasterApprover.setApprover(schoolMasterApprover);
schoolMasterApprover.setApprover(departmentApprover);
departmentApprover.processRequest(purchaseRequest);
viceSchoolMasterApprover.processRequest(purchaseRequest);
}
}
Notes and Details of the Chain of Responsibility Pattern
1) Separate the request and processing to achieve decoupling and improve the flexibility of the system
2) Simplify the object so that the object does not need to know the structure of the chain
3) The performance will be affected, especially when the chain is relatively long, so it needs to be controlled The maximum number of nodes in the chain, generally by setting a maximum number of nodes in the Handler, and judging whether it has exceeded the threshold in the setNext() method, if it exceeds, the chain is not allowed to be established, so as to avoid the occurrence of super long chains that will unintentionally damage the system performance