-
Notifications
You must be signed in to change notification settings - Fork 1
/
3-8-strategy.vala
45 lines (34 loc) · 964 Bytes
/
3-8-strategy.vala
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
interface SortStrategy : Object {
public abstract int[] sort (int[] dataset);
}
class BubbleSortStrategy : Object, SortStrategy {
public int[] sort (int[] dataset) {
print ("Sorting using bubble sort\n");
//do sorting
return dataset;
}
}
class QuickSortStrategy : Object, SortStrategy {
public int[] sort (int[] dataset) {
print ("Sorting using quick sort\n");
//do sorting
return dataset;
}
}
class Sorter {
protected SortStrategy sorter;
public Sorter (SortStrategy sorter) {
this.sorter = sorter;
}
public int[] sort (int[] dataset) {
return sorter.sort (dataset);
}
}
public int main (string[] args) {
int[] dataset = {1, 5, 4, 3 ,2, 8};
var sorter = new Sorter (new BubbleSortStrategy ());
sorter.sort (dataset);
sorter = new Sorter (new QuickSortStrategy ());
sorter.sort (dataset);
return 0;
}