Skip to content

Commit e391990

Browse files
authored
Merge branch 'master' into dev
2 parents ab50994 + 334faef commit e391990

File tree

17 files changed

+393
-4
lines changed

17 files changed

+393
-4
lines changed

.idea/markdown-exported-files.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# ExamplesOfDesignPatterns
22
## 序言
33

4-
各种设计模式的Uml图、分析与代码实现。所有模式的Uml图都在[XMindDescribe](https://github.com/mirsfang/ExamplesOfDesignPatterns/tree/master/XMindDescribe)目录下的xmind文件里
5-
6-
4+
各种设计模式的Uml图、分析与代码实现,主要在dev分支。 所有模式的Uml图都在[XMindDescribe](https://github.com/mirsfang/ExamplesOfDesignPatterns/tree/master/XMindDescribe)目录下的xmind文件里
75

86
## 项目结构
97

@@ -27,9 +25,15 @@
2725
* 代理模式
2826
* 装饰器模式
2927
* ​访问者模式
28+
* 观察者模式
29+
* 责任链模式
30+
3031

3132

3233

34+
## XMind文件截图
3335

36+
[点击前往](https://github.com/mirsfang/ExamplesOfDesignPatterns/blob/master/XMind%E6%88%AA%E5%9B%BE.png)
3437

3538

39+
交流群:38240957

XMind截图.png

224 KB
Loading

src/action/action.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
| 包名 | 描述 |
44
| ------- | ----- |
55
| visitor | 访问者模式 |
6+
|observer|观察者模式|
7+
68

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package action.chainOfResponsibility;/**
2+
* Created by MirsFang on 2017/3/6.
3+
*/
4+
5+
import action.chainOfResponsibility.abs.Handler;
6+
7+
/***
8+
*作者:MirsFang
9+
*模式:责任链模式
10+
*时间:2017/03/06/下午12:45
11+
*备注 公司老板,方老板
12+
***/
13+
14+
public class BossHandler extends Handler {
15+
16+
@Override
17+
public int getLevel() {
18+
return 3;
19+
}
20+
21+
@Override
22+
public String getnName() {
23+
return "方老板";
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package action.chainOfResponsibility;/**
2+
* Created by MirsFang on 2017/3/6.
3+
*/
4+
5+
import action.chainOfResponsibility.abs.Handler;
6+
7+
/***
8+
*作者:MirsFang
9+
*模式:责任链模式
10+
*时间:2017/03/06/下午12:47
11+
*备注 准备开始走流程
12+
***/
13+
14+
public class ChainOfReponsibilityMain {
15+
16+
public static void main(String[] args) {
17+
//我要报销的钱数;
18+
int money=5;
19+
//初始化各个处理者
20+
Handler manager = new ManagerHandler();
21+
Handler finance = new FinanceHandler();
22+
Handler boss = new BossHandler();
23+
//设置链中的顺序
24+
manager.setNextHandler(finance);
25+
finance.setNextHandler(boss);
26+
//开始请求
27+
System.out.println("我要报销"+money+"元");
28+
if (manager.handlerRequst(money)) {
29+
System.out.println("报销成功");
30+
} else {
31+
System.out.println("报销失败");
32+
}
33+
}
34+
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package action.chainOfResponsibility;/**
2+
* Created by MirsFang on 2017/3/6.
3+
*/
4+
5+
import action.chainOfResponsibility.abs.Handler;
6+
7+
/***
8+
*作者:MirsFang
9+
*模式:责任链模式
10+
*时间:2017/03/06/下午12:45
11+
*备注 公司财务,李财务
12+
***/
13+
14+
public class FinanceHandler extends Handler {
15+
16+
@Override
17+
public int getLevel() {
18+
return 2;
19+
}
20+
21+
@Override
22+
public String getnName() {
23+
return "李财务";
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package action.chainOfResponsibility;/**
2+
* Created by MirsFang on 2017/3/6.
3+
*/
4+
5+
import action.chainOfResponsibility.abs.Handler;
6+
7+
/***
8+
*作者:MirsFang
9+
*模式:责任链模式
10+
*时间:2017/03/06/下午12:45
11+
*备注 你的上司,牛经理
12+
***/
13+
14+
public class ManagerHandler extends Handler {
15+
16+
@Override
17+
public int getLevel() {
18+
return 1;
19+
}
20+
21+
@Override
22+
public String getnName() {
23+
return "牛经理";
24+
}
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package action.chainOfResponsibility.abs;/**
2+
* Created by MirsFang on 2017/3/6.
3+
*/
4+
5+
/***
6+
*作者:MirsFang
7+
*模式:责任链模式
8+
*时间:2017/03/06/下午12:33
9+
*备注 抽象的执行者
10+
*
11+
***/
12+
13+
public abstract class Handler {
14+
//下一个执行者
15+
private Handler nextHandler;
16+
17+
18+
//当前执行者对请求做处理
19+
public final boolean handlerRequst(int handLeve){
20+
boolean bReq=false;
21+
//判断自己是否可以审核
22+
if(getLevel()>=handLeve){
23+
System.out.println(getnName()+" :可以审批");
24+
return true;
25+
}else {
26+
if(this.nextHandler!=null){
27+
System.out.println(getnName()+"向"+nextHandler.getnName()+"递交请求");
28+
//向上一级请求
29+
bReq=nextHandler.handlerRequst(handLeve);
30+
//根据上级的答复做出回应
31+
if(bReq){
32+
System.out.println(getnName()+" :"+nextHandler.getnName()+"说可以审批");
33+
}else {
34+
System.out.println(getnName()+" :"+nextHandler.getnName()+"说不可以审批");
35+
}
36+
}else {
37+
//到这边已经没有下一个处理者了
38+
System.out.println(getnName()+" :金额太大了");
39+
}
40+
}
41+
return bReq;
42+
}
43+
44+
45+
public void setNextHandler(Handler nextHandler) {
46+
this.nextHandler = nextHandler;
47+
}
48+
//可以审批的金额
49+
public abstract int getLevel();
50+
public abstract String getnName();
51+
52+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 责任链模式
2+
3+
相信大家对责任链模式都很熟悉,多多少收都知道点,这次我们拿公司的财务报销来描述责任链模式,
4+
5+
> 使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止。
6+
7+
8+
9+
| 类名 | 描述 |
10+
| ------------------------ | ----------- |
11+
| Handler | 抽象处理者 |
12+
| ManagerHandler | 具体处理者(你的上司) |
13+
| FinanceHandler | 具体处理者(公司) |
14+
| BossHandler | 具体处理者(公司老板) |
15+
| ChainOfReponsibilityMain | 执行类(主方法—>你) |
16+
17+
假设 上司的能审批的金额level为 1,财务的level 为2,boss的level为3
18+
19+
20+
21+
责任链模式是一个运用递归思想来解决问题的模式,看代码就是像链表一样,优点是将请求和处理分开,请求者不必知道处理者是谁,处理者也可以不用知道请求的全貌,缺点也就比较明显:一是性能问题(遍历),二是调试不方便

0 commit comments

Comments
 (0)