-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS20_Stategy.java
More file actions
56 lines (40 loc) · 997 Bytes
/
DS20_Stategy.java
File metadata and controls
56 lines (40 loc) · 997 Bytes
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
//Strategy
abstract class Strategy {
public abstract void method();
}
//ConcreteStrategy
class strategyImplA extends Strategy {
public void method() {
System.out.println("这是第一个实现");
}
}
class StrategyImplB extends Strategy {
public void method() {
System.out.println("这是第二个实现");
}
}
class StrategyImplC extends Strategy {
public void method() {
System.out.println("这是第三个实现");
}
}
//Context
class Context {
Strategy stra;
public Context(Strategy stra) {
this.stra = stra;
}
public void doMethod() {
stra.method();
}
}
public class DS20_Stategy {
public static void main(String[] args) {
Context ctx = new Context(new strategyImplA());
ctx.doMethod();
ctx = new Context(new StrategyImplB());
ctx.doMethod();
ctx = new Context(new StrategyImplC());
ctx.doMethod();
}
}