策略模式(Strategy)

Posted by Jfson on 2018-06-22

策略模式允许您根据情况切换算法或策略。

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
interface Sort{
void sort();
}
class QuickSort implements Sort{
@Override
public void sort() {
System.out.print("快排");
}
}
class BubbleSort implements Sort{
@Override
public void sort() {
System.out.print("冒泡");
}
}
class Strategy{
Sort mSort;
public void sort(int[] res){
if (res.length > 1000 * 1000){
mSort = new QuickSort();
}else {
mSort = new BubbleSort();
}
mSort.sort();
}
}

pv UV: