-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathStrategyPattern.java
More file actions
60 lines (48 loc) · 1.05 KB
/
StrategyPattern.java
File metadata and controls
60 lines (48 loc) · 1.05 KB
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
/**
* @Project:
* @Author: leegoo
* @Date: 2019年07月11日
*/
package cn.withme.pattern;
/**
* ClassName: StrategyPattern
* @Description:策略模式
*
* 如果不使用策略模式
* if - else
* 那么
*
*
* @author leegoo
* @date 2019年07月11日
*/
public class StrategyPattern {
public interface Eat {
String eatFruit ();
}
public static class Apple implements Eat{
// @Override
public String eatFruit() {
return "吃了一个苹果";
}
}
public static class Banan implements Eat{
// @Override
public String eatFruit() {
return "吃了一个香蕉";
}
}
public static class Strategy {
private final Eat eat;
public Strategy(Eat eat) {
this.eat = eat;
}
public String eatFruit() {
return eat.eatFruit();
}
}
public static void main(String[] args) {
Strategy strategy = new Strategy(new Apple());
System.out.println(strategy.eatFruit());
}
}