-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStrategy.dart
40 lines (31 loc) · 1.01 KB
/
Strategy.dart
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
import 'package:design_pattern_dart/Display/Example.dart';
class Strategy extends Example {
Strategy([String filePath = "lib/Behavioral/Strategy.dart"])
: super(filePath);
@override
String testRun() {
Sorter sorter = Sorter(MergeSortStrategy());
var result1 = sorter.sort([1, 2, 3]);
sorter = Sorter(QuickSortStrategy());
var result2 = sorter.sort([1, 2, 3]);
return "$result1 \n$result2";
}
}
// 同樣是 sort method ,我們在不同情況可能會使用 merge sort ,有的情況會使用 quick sort
abstract class SortStrategy {
String sort(List array);
}
class MergeSortStrategy implements SortStrategy {
@override
String sort(List array) => "Sort using merge sort";
}
class QuickSortStrategy implements SortStrategy {
@override
String sort(List array) => "Sort using quick sort";
}
// 這時候策略模式就可以動態決定要使用哪一種方法實作
class Sorter {
SortStrategy _sorter;
Sorter(this._sorter);
String sort(List array) => _sorter.sort(array);
}