-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Sorting with the Strategy Pattern | ||
|
||
In this exercise, we want to implement sorting algorithms and choose them based on runtime specific variables. | ||
|
||
### Part 1: Sorting | ||
|
||
First, we need to implement two sorting algorithms, in this case `MergeSort` and `BubbleSort`. | ||
|
||
**You have the following tasks:** | ||
|
||
1. [task][Implement Bubble Sort](artemis/test/structural.TestBubbleSort,artemis/test/behavior.TestBubbleSort) | ||
Implement the method `PerformSort([]time.Time)` in the class `BubbleSort`. Make sure to follow the Bubble Sort algorithm exactly. | ||
|
||
2. [task][Implement Merge Sort](artemis/test/structural.TestMergeSort,artemis/test/behavior.TestMergeSort) | ||
Implement the method `PerformSort([]time.Time)` in the class `MergeSort`. Make sure to follow the Merge Sort algorithm exactly. | ||
|
||
### Part 2: Strategy Pattern | ||
|
||
We want the application to apply different algorithms for sorting a slice of `time.Time` objects. | ||
Use the strategy pattern to select the right sorting algorithm at runtime. | ||
|
||
**You have the following tasks:** | ||
|
||
1. SortStrategy Interface | ||
Create a `SortStrategy` interface and adjust the sorting algorithms so that they implement this interface. | ||
|
||
2. [task][Context Class](artemis/test/structural.TestContext) | ||
Create and implement a `Context` class following the below class diagram | ||
|
||
3. [task][Context Policy](artemis/test/structural.TestPolicy) | ||
Create and implement a `Policy` class following the below class diagram with a simple configuration mechanism: | ||
|
||
1. [task][Select MergeSort](artemis/test/behavior.TestUseMergeSortForBigList) | ||
Select `MergeSort` when the slice has more than 10 dates. | ||
|
||
2. [task][Select BubbleSort](artemis/test/behavior.TestUseMergeSortForBigList) | ||
Select `BubbleSort` when the slice has less or equal 10 dates. | ||
|
||
4. Complete the `Client` class which demonstrates switching between two strategies at runtime. | ||
|
||
@startuml | ||
|
||
class Client { | ||
} | ||
|
||
class Policy ##testsColor(artemis/test/structural.TestPolicy) { | ||
<color:testsColor(artemis/test/behavior.TestUseMergeSortForBigList,artemis/test/behavior.TestUseMergeSortForBigList)>+Configure()</color> | ||
} | ||
|
||
class Context ##testsColor(artemis/test/structural.TestContext) { | ||
-dates: []time.Time | ||
+Sort() | ||
} | ||
|
||
interface SortStrategy { | ||
+PerformSort([]time.Time) | ||
} | ||
|
||
class BubbleSort ##testsColor(artemis/test/structural.TestBubbleSort) { | ||
<color:testsColor(artemis/test/behavior.TestBubbleSort)>+PerformSort([]time.Time)</color> | ||
} | ||
|
||
class MergeSort ##testsColor(artemis/test/structural.TestMergeSort) { | ||
<color:testsColor(artemis/test/behavior.TestMergeSort)>+PerformSort([]time.Time)</color> | ||
} | ||
|
||
MergeSort -up-|> SortStrategy | ||
BubbleSort -up-|> SortStrategy | ||
Policy -right-> Context #testsColor(artemis/test/structural.TestPolicy): context | ||
Context -right-> SortStrategy #testsColor(artemis/test/structural.TestContext): sortAlgorithm | ||
Client .down.> Policy | ||
Client .down.> Context | ||
|
||
hide empty fields | ||
hide empty methods | ||
|
||
@enduml | ||
|
||
|
||
### Part 3: Optional Challenges | ||
|
||
(These are not tested) | ||
|
||
1. Create a new class `QuickSort` that implements `SortStrategy` and implement the Quick Sort algorithm. | ||
|
||
2. Think about a useful decision in `Policy` when to use the new `QuickSort` algorithm. |