-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrategyTest.java
61 lines (47 loc) · 1.01 KB
/
StrategyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package JavaPatternDemos;
/**
*
* 策略模式
*
* 旨在对策略进行管理,封装
* 状态的改变,或者说策略的选择一般是主观设置
*
*
*/
public class StrategyTest {
public static void main(String[] args) {
}
}
class SContext{
private int num1 = 1;
}
class SResult{
private int num2 = 0;
}
class StrategyManager{
private SContext curContext;
private SResult curResult;
private Strategy curStrategy;
public void setCurStrategy(Strategy curStrategy) {
this.curStrategy = curStrategy;
}
public void run(){
this.curStrategy.do_strategy(curContext);
}
}
interface Strategy{
// 也可以用继承
public void do_strategy(SContext scont);
}
class GoodStrategy implements Strategy{
@Override
public void do_strategy(SContext scont) {
// do something , get results
}
}
class BadStrategy implements Strategy{
@Override
public void do_strategy(SContext scont) {
// do something , get results
}
}