Skip to content

Commit d8a2e97

Browse files
committed
chain
1 parent 1ca7796 commit d8a2e97

8 files changed

Lines changed: 154 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
9. [日报表格只有一份---单例模式](http://blog.lmj.wiki/2016/12/20/design-pattern/singleton/)
1414
10. [统一热干面的制作流程---模板方法](http://blog.lmj.wiki/2016/12/22/design-pattern/template_method/)
1515
11. [梳理公司的组织架构---组合模式](http://blog.lmj.wiki/2017/01/18/design-pattern/composite/)
16+
12. [采购申请的处理流程---责任链模式](http://blog.lmj.wiki/2017/02/22/design-pattern/chain/)
1617

1718

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.anly.designpattern.chain;
2+
3+
/**
4+
* Created by mingjun on 17/2/20.
5+
*/
6+
public class Buyer {
7+
8+
public static void main(String[] args) {
9+
10+
// 小需求
11+
Request smallRequest = new Request("10箱饮料");
12+
RequestFlow.getRequestChain(RequestFlow.TYPE_SMALL_REQUEST).handleRequest(smallRequest);
13+
14+
// 一般需求
15+
Request normalRequest = new Request("10套桌椅");
16+
RequestFlow.getRequestChain(RequestFlow.TYPE_NORMAL_REQUEST).handleRequest(normalRequest);
17+
18+
// 大需求
19+
Request bigRequest = new Request("一套同步电子显示大屏");
20+
RequestFlow.getRequestChain(RequestFlow.TYPE_BIG_REQUEST).handleRequest(bigRequest);
21+
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.anly.designpattern.chain;
2+
3+
/**
4+
* Created by mingjun on 17/2/20.
5+
*/
6+
public class GeneralManager extends RequestHandler {
7+
8+
public GeneralManager(RequestHandler next) {
9+
super(next);
10+
}
11+
12+
@Override
13+
public String toString() {
14+
return "总经理";
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.anly.designpattern.chain;
2+
3+
/**
4+
* Created by mingjun on 17/2/20.
5+
*/
6+
public class PurchasingManager extends RequestHandler {
7+
8+
/**
9+
* 构造时, 传入下一个处理人
10+
* @param next 下一个处理人
11+
*/
12+
public PurchasingManager(RequestHandler next) {
13+
super(next);
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return "采购经理";
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.anly.designpattern.chain;
2+
3+
/**
4+
* Created by mingjun on 17/2/20.
5+
*/
6+
public class Request {
7+
8+
public String name;
9+
10+
public Request(String name) {
11+
this.name = name;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return name + "采购申请";
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.anly.designpattern.chain;
2+
3+
/**
4+
* Created by mingjun on 17/2/21.
5+
*/
6+
public class RequestFlow {
7+
8+
public static final int TYPE_SMALL_REQUEST = 1;
9+
public static final int TYPE_NORMAL_REQUEST = 2;
10+
public static final int TYPE_BIG_REQUEST = 3;
11+
12+
public static RequestHandler getRequestChain(int type) {
13+
switch (type) {
14+
// 小物件, 只需采购经理审批
15+
case TYPE_SMALL_REQUEST:
16+
return new PurchasingManager(null);
17+
18+
// 一般物件, 需要总经理审批
19+
case TYPE_NORMAL_REQUEST:
20+
return new PurchasingManager(new GeneralManager(null));
21+
22+
// 大件物品, 需要小光审批
23+
case TYPE_BIG_REQUEST:
24+
default:
25+
return new PurchasingManager(new GeneralManager(new XiaoGuang(null)));
26+
}
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.anly.designpattern.chain;
2+
3+
/**
4+
* Created by mingjun on 17/2/20.
5+
*/
6+
public abstract class RequestHandler {
7+
8+
// 上一级的处理人员
9+
private RequestHandler mNext;
10+
11+
public RequestHandler(RequestHandler next) {
12+
this.mNext = next;
13+
}
14+
15+
/**
16+
* 处理采购需求
17+
* @param req
18+
*/
19+
public void handleRequest(Request req) {
20+
printHandling(req);
21+
if (mNext != null) {
22+
mNext.handleRequest(req);
23+
}
24+
}
25+
26+
protected void printHandling(Request req) {
27+
System.out.println(this.toString() + "审批了:" + req);
28+
}
29+
30+
@Override
31+
public abstract String toString();
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.anly.designpattern.chain;
2+
3+
/**
4+
* Created by mingjun on 17/2/20.
5+
*/
6+
public class XiaoGuang extends RequestHandler {
7+
8+
public XiaoGuang(RequestHandler next) {
9+
super(next);
10+
}
11+
12+
@Override
13+
public String toString() {
14+
return "小光";
15+
}
16+
}

0 commit comments

Comments
 (0)