一.案例引入
1.案例描述
2.传统设计方案
3.传统方式评价
二.职责链模式
1.基本介绍
2.原理类图
说明
3.职责链模式解决上述问题的原理类图
4.具体代码
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) ;
}
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) ;
}
}
}
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) ;
}
}
}
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) ;
}
}
}
PurchaseRequest
public class PurchaseRequest {
private int type;
private float price = 0.0 f;
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;
}
}
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) ;
}
}
5.注意事项和细节