一、职责链模式
1、基本介绍
(1)职责链模式(Chain of Responsibility Pattern),又叫责任链模式,为请求创建了一个接收者对象的链(简单示意图)。这种模式对请求的发送者和接受者进行解耦。
(2)职责链模式通常每个接收者对象都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依次类推。
(3)这种类型的设计模式属于行为型模式;
2、原理类图
职责链模式(Chain of Responsibility)使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连城一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
对原理类图的说明——即(职责链模式的角色及职责)
(1)Handler:抽象的处理者,定义了一个处理请求的接口,同时包含另外一个 Handler;
(2)ConcreteHandlerA,B 是具体的处理者,处理它自己负责的请求,可以访问它的后继者(即下一个处理者),如果可以处理当前请求,则处理,否则就将该请求交给 后继者去处理,从而形成一个职责链;
(3)Request:含有很多属性,表示一个请求;
二、职责链模式完成OA系统采购审批
1、思路分析和图解(类图)
2、代码实现
抽象审批类:
1 /**
2 * 抽象的审批者
3 */
4 public abstract class Approver {
5
6 //下一个处理者
7 Approver approver;
8
9 //名字
10 String name;
11
12 public Approver(String name) {
13 this.name = name;
14 }
15
16 /**
17 * 下一个处理者
18 * @param approver
19 */
20 public void setApprover(Approver approver) {
21 this.approver = approver;
22 }
23
24
25 /**
26 * 处理审批请求的方法,得到一个请求,
27 * 处理是子类完成,因此该方法做成抽象
28 * @param purchaseRequest
29 */
30 public abstract void process(PurchaseRequest purchaseRequest);
31 }
各个审批者(处理实现类):
1 public class DepartmentApprover extends Approver {
2
3
4 public DepartmentApprover(String name) {
5 super(name);
6 }
7
8 @Override
9 public void process(PurchaseRequest purchaseRequest) {
10 if (purchaseRequest.getPrice() <= 5000) {
11 System.out.println("请求编号 id=" + purchaseRequest.getId() + "被" + this.name + "处理了。");
12 } else {
13 approver.process(purchaseRequest);
14 }
15 }
16 }
17 ------------------------------------------------------------------
18 public class CollegeApprover extends Approver{
19
20 public CollegeApprover(String name) {
21 super(name);
22 }
23
24 @Override
25 public void process(PurchaseRequest purchaseRequest) {
26 if (purchaseRequest.getPrice() > 5000 && purchaseRequest.getPrice() <= 10000) {
27 System.out.println("请求编号 id=" + purchaseRequest.getId() + "被" + this.name + "处理了。");
28 } else {
29 approver.process(purchaseRequest);
30 }
31 }
32 }
33 ------------------------------------------------------------------
34 public class ViceSchoolMasterApprover extends Approver{
35 public ViceSchoolMasterApprover(String name) {
36 super(name);
37 }
38
39 @Override
40 public void process(PurchaseRequest purchaseRequest) {
41 if (purchaseRequest.getPrice() > 10000 && purchaseRequest.getPrice() <= 30000) {
42 System.out.println("请求编号 id=" + purchaseRequest.getId() + "被" + this.name + "处理了。");
43 } else {
44 approver.process(purchaseRequest);
45 }
46 }
47 }
48 ------------------------------------------------------------------
49 public class SchoolMasterApprover extends Approver{
50
51 public SchoolMasterApprover(String name) {
52 super(name);
53 }
54
55 @Override
56 public void process(PurchaseRequest purchaseRequest) {
57 if (purchaseRequest.getPrice() > 30000) {
58 System.out.println("请求编号 id=" + purchaseRequest.getId() + "被" + this.name + "处理了。");
59 } else {
60 approver.process(purchaseRequest);
61 }
62 }
63 }
请求类:
1 /**
2 * 请求类
3 */
4 public class PurchaseRequest {
5
6 //请求类型
7 private int type = 0;
8
9 //请求金额
10 private float price = 0.0f;
11
12 private int id = 0;
13
14 /**
15 * 构造器
16 * @param type
17 * @param price
18 * @param id
19 */
20 public PurchaseRequest(int type, float price, int id) {
21 this.type = type;
22 this.price = price;
23 this.id = id;
24 }
25
26 public int getType() {
27 return type;
28 }
29
30 public float getPrice() {
31 return price;
32 }
33
34 public int getId() {
35 return id;
36 }
37 }
客户端:
1 public class Client {
2
3 public static void main(String[] args) {
4 //创建一个请求
5 PurchaseRequest purchaseRequest = new PurchaseRequest(1, 31000, 1);
6
7 //创建相关的审核者
8 DepartmentApprover departmentApprover = new DepartmentApprover("张主任");
9
10 CollegeApprover collegeApprover = new CollegeApprover("李院长");
11
12 ViceSchoolMasterApprover viceSchoolMasterApprover = new ViceSchoolMasterApprover("王副院长");
13
14 SchoolMasterApprover schoolMasterApprover = new SchoolMasterApprover("黄校长");
15
16
17 //需要将各个审批级别的下一个审批者处理好(处理人构成环形:)
18 departmentApprover.setApprover(collegeApprover);
19 collegeApprover.setApprover(viceSchoolMasterApprover);
20 viceSchoolMasterApprover.setApprover(schoolMasterApprover);
21 schoolMasterApprover.setApprover(departmentApprover);
22
23
24 departmentApprover.process(purchaseRequest);
25
26
27 }
28 }