'Designing Strategy for a strategy
For simplicity, I will use a simple example for my question. Let's consider the below example as an implementation of strategy pattern for finding min and max values of an array. Strategy goes as follows:
interface FindingStrategy {
int find(int[] numbers);
}
class MaxFindingStrategy implements FindingStrategy {
public int find(int[] numbers) {
if (numbers.length == 0) return Integer.MAX_VALUE;
return numbers[numbers.length - 1];
}
}
class MinFindingStrategy implements FindingStrategy {
public int find(int[] numbers) {
if (numbers.length == 0) return Integer.MIN_VALUE;
return numbers[0];
}
}
class Finder {
private FindingStrategy strategy;
public Finder(FindingStrategy strategy) {
this.strategy = strategy;
}
public int find(int[] numbers) {
// TODO: Sorting
return this.strategy.find(numbers);
}
}
As we use int array (suppose we are limited to this type), we have to sort it to be able to use numbers[0] and numbers[number.length - 1] and suppose we care about how or what algorithm is used to sort the array.
To do that we can introduce a sorting algorithm before finding min and max, and as we have multiple algorithms as a sorting algorithm, this fits Strategy Pattern but we already applied strategy pattering for finding min and max.
What would be best in terms of design to use a strategy for different sorting algorithms (say insertion, selection, bubble, merge, and quick) on top of min and max strategies?
What if we use another strategy under Finder class, call it SortingStrategy, as follows:
interface SortingStrategy {
int[] sort(int[] numbers);
}
class InsertionSortStrategy implements SortingStrategy {
public int[] sort(int[] numbers) {
// sorting code
}
}
class SelectionSortStrategy implements SortingStrategy {
public int[] sort(int[] numbers) {
// sorting code
}
}
class BubbleSortStrategy implements SortingStrategy {
public int[] sort(int[] numbers) {
// sorting code
}
}
class MergeSortStrategy implements SortingStrategy {
public int[] sort(int[] numbers) {
// sorting code
}
}
class QuickSort implements SortingStrategy {
public int[] sort(int[] numbers) {
// sorting code
}
}
class Finder {
private FindingStrategy findingStrategy;
private SortingStrategy sortingStrategy;
public Finder(FindingStrategy findingStrategy, SortingStrategy sortingStrategy) {
this.findingStrategy = findingStrategy;
this.sortingStrategy = sortingStrategy;
}
public int find(int[] numbers) {
numbers = this.sortingStrategy.sort(numbers);
return this.findingStrategy.find(numbers);
}
}
and in client code we do:
Finder = new Finder(new MinFindingStrategy(), new BubbleSortStrategy());
Please answer below questions:
- Is this considered as best practice for strategy pattern? what solutions we can approach this problem with and what would be the best one in terms of design?
- If we make Sorting strategy under Finding strategy as
Finder(new MinFindingStrategy(new BubbleSortStrategy()));is this okay? why if not? - What better name we call this problem?
Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
