Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e83e9dc

Browse files
author
deeper
committed
add strategy
1 parent 8b418cf commit e83e9dc

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

‎src/com/ruoxu/pattern/state/Demo.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ruoxu.pattern.state;
2+
/**
3+
* 状态模式
4+
* 注意: 策略模式和状态模式 的结构几乎完全一样,但是他们的目的,本质却不一样。
5+
* 策略模式的行为是彼此独立,可相互替换的。
6+
* 状态模式的行为是平行,不可替换的。
7+
*
8+
*
9+
*
10+
*/
11+
public class Demo {
12+
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.ruoxu.pattern.strategy;
2+
3+
public class BusPolicy implements CalculatePolicy{
4+
5+
/**
6+
* 北京公交车,10公里之内1元钱,超过10公里之后每加一元钱可以乘坐5公里
7+
* @param km
8+
* @return
9+
*/
10+
@Override
11+
public int calculatePrice(int km) {
12+
int extraTotal = km - 10;
13+
int extraFactor = extraTotal / 5;
14+
int fraction = extraTotal % 5;
15+
int price = 1 + extraFactor * 1;
16+
return fraction > 0? ++price : price;
17+
}
18+
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.ruoxu.pattern.strategy;
2+
3+
public interface CalculatePolicy {
4+
/**
5+
* 按照距离来计算价格
6+
* @param km 公里
7+
* @return 返回价格
8+
*/
9+
int calculatePrice(int km);
10+
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.ruoxu.pattern.strategy;
2+
/**
3+
* 策略模式
4+
* 注意: 策略模式和状态模式 的结构几乎完全一样,但是他们的目的,本质却不一样。
5+
* 策略模式的行为是彼此独立,可相互替换的。
6+
* 状态模式的行为是平行,不可替换的。
7+
*
8+
* 定义:
9+
* 策略模式定义了一系列的算法,并将每一种算法封装起来,并且使他们可以相互替换,策略模式让算法独立于使用它的客户而独立变化。
10+
*
11+
* 使用场景:
12+
* 针对同一类型问题的多种处理方式,仅仅是具体行为有差别
13+
* 需要安全地封装多种同一类型的操作时
14+
* 出现同一抽象类有多个子类,而又需要使用if-else或者switch来选择子类时
15+
*
16+
*/
17+
public class Demo {
18+
public static void main(String[] args) {
19+
int num = 6;
20+
TranficCalculator calculator = new TranficCalculator();
21+
calculator.setPolicy(new BusPolicy());
22+
int price = calculator.calculatePrice(num);
23+
System.out.println("公交车乘"+num+"公里的价格是:"+price);
24+
25+
26+
27+
28+
29+
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.ruoxu.pattern.strategy;
2+
3+
public class SubwayPolicy implements CalculatePolicy{
4+
5+
/**
6+
* 6公里(含)内3元,6~12公里(含)4元;12~22公里(含)5元,22~32公里(含)6元。
7+
* @param km
8+
* @return
9+
*/
10+
@Override
11+
public int calculatePrice(int km) {
12+
if(km < 6){
13+
return 3;
14+
}else if(km > 6 && km < 12){
15+
return 4;
16+
}else if(km > 12 && km <22){
17+
return 5;
18+
}else if(km > 22 && km <32){
19+
return 6;
20+
}
21+
// 其他距离简化为7
22+
return 7;
23+
}
24+
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ruoxu.pattern.strategy;
2+
/**
3+
* 公交车出行价格计算器
4+
* 讲策略注入到TranficCalculator 中
5+
*/
6+
public class TranficCalculator {
7+
private CalculatePolicy policy ;
8+
9+
public void setPolicy(CalculatePolicy policy){
10+
this.policy = policy;
11+
}
12+
13+
public int calculatePrice(int km){
14+
return policy.calculatePrice(km);
15+
}
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /