策略模式
定义一组算法,并封装好其中每一个,使它们可以互相替换。策略模式使算法的变化独立于使用它的客户。
一个 redis 中的例子:redis 内存写满之后会通过不同的策略将数据删除,继续提供服务。使用策略模式模拟redis中的4中淘汰策略(random、ttl、lru、lfu)
public interface EvictionStrategy {
/**
* 筛选出淘汰的 key
*/
List<String> selectKeys();
}
public class LfuStrategy implements EvictionStrategy {
@Override
public List<String> selectKeys() {
System.out.println("使用 lfu 淘汰策略");
return Collections.singletonList("lfu_key");
}
}
public class LruStrategy implements EvictionStrategy {
@Override
public List<String> selectKeys() {
System.out.println("使用 lru 淘汰策略");
return Collections.singletonList("lru_key");
}
}
public class RandomStrategy implements EvictionStrategy {
@Override
public List<String> selectKeys() {
System.out.println("使用 random 淘汰策略");
return Collections.singletonList("random_key");
}
}
public class TtlStrategy implements EvictionStrategy {
@Override
public List<String> selectKeys() {
System.out.println("使用 ttl 淘汰策略");
return Collections.singletonList("ttl_key");
}
}
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
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
public class EvictionStrategyFactory {
private static final Map<String, EvictionStrategy> STRATEGY_MAP = new HashMap<>();
static {
STRATEGY_MAP.put("ttl", new TtlStrategy());
STRATEGY_MAP.put("lru", new LruStrategy());
STRATEGY_MAP.put("lfu", new LfuStrategy());
STRATEGY_MAP.put("random", new RandomStrategy());
}
public static EvictionStrategy getStrategy(String type) {
return STRATEGY_MAP.get(type);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class RedisServer {
private EvictionStrategy strategy;
public RedisServer(final EvictionStrategy strategy) {
this.strategy = strategy;
}
// 根据策略淘汰缓存
public void clean() {
List<String> keys = strategy.selectKeys();
System.out.println("淘汰缓存: " + keys);
}
// 动态修改缓存淘汰策略
public void changeStrategy(String type) {
System.out.println("修改缓存策略: " + type);
strategy = EvictionStrategyFactory.getStrategy(type);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
测试类
public class StrategyApp {
public static void main(String[] args) {
String type = getTypeFromConfig("redis.conf");
EvictionStrategy strategy = EvictionStrategyFactory.getStrategy(type);
RedisServer server = new RedisServer(strategy);
server.clean();
server.changeStrategy("lfu");
server.clean();
}
private static String getTypeFromConfig(String configFilePath) {
return "lru";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Last Updated: 2024/04/23, 01:30:37