Skip to content

Instantly share code, notes, and snippets.

@SiAust
Last active May 25, 2020 13:28
Show Gist options
  • Save SiAust/a10202f8a75cd2b13cb135266c550fe2 to your computer and use it in GitHub Desktop.
Save SiAust/a10202f8a75cd2b13cb135266c550fe2 to your computer and use it in GitHub Desktop.
Strategy design pattern.
public class Main {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
final String[] elements = scanner.nextLine().split("\\s+");
int[] numbers = null;
if (elements[0].equals("EMPTY")) {
numbers = new int[0];
} else {
numbers = new int[elements.length];
for (int i = 0; i < elements.length; i++) {
numbers[i] = Integer.parseInt(elements[i]);
}
}
final String type = scanner.nextLine();
Finder finder = null;
switch (type) {
case "MIN":
finder = new Finder(new MinFindingStrategy());
break;
case "MAX":
finder = new Finder(new MaxFindingStrategy());
break;
default:
break;
}
if (finder == null) {
throw new RuntimeException(
"Unknown strategy type passed. Please, write to the author of the problem.");
}
System.out.println(finder.find(numbers));
}
}
class Finder {
private FindingStrategy strategy;
public Finder(FindingStrategy strategy) {
this.strategy = strategy;
}
/**
* It performs the search algorithm according to the given strategy
*/
public int find(int[] numbers) {
return this.strategy.getResult(numbers);
}
}
interface FindingStrategy {
/**
* Returns search result
*/
int getResult(int[] numbers);
}
class MaxFindingStrategy implements FindingStrategy {
public int getResult(int[] numbers) {
if (numbers.length == 0) {
return Integer.MAX_VALUE;
}
Arrays.sort(numbers);
return numbers[numbers.length -1];
}
}
class MinFindingStrategy implements FindingStrategy {
public int getResult(int[] numbers) {
if (numbers.length == 0) {
return Integer.MIN_VALUE;
}
Arrays.sort(numbers);
return numbers[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment