Skip to content

Instantly share code, notes, and snippets.

@joshiraez
Created January 1, 2020 04:22
Show Gist options
  • Save joshiraez/c57367e8eebfc732c60df5d94b935150 to your computer and use it in GitHub Desktop.
Save joshiraez/c57367e8eebfc732c60df5d94b935150 to your computer and use it in GitHub Desktop.
Transforming it to an abstract approach instead of interface approach (having some part of the code already implemented).
class OperationAbstractAsClass implements Operation {
Predicate<String> isOperation;
IntBinaryOperator reductionOperation;
private OperationAbstractAsClass(){};
public static OperationAbstractAsClass of(
Predicate<String> isOperation,
IntBinaryOperator reductionOperation
){
OperationAbstractAsClass toBuild = new OperationAbstractAsClass();
toBuild.isOperation = isOperation;
toBuild.reductionOperation = reductionOperation;
return toBuild;
}
@Override
public boolean isOperation(final String input) {
return this.isOperation.test(input);
}
@Override
public int operate(final List<Integer> numbers) {
return this.operateWithGivenReductionOperation(numbers);
}
private int operateWithGivenReductionOperation(final List<Integer> numbers) {
return numbers
.stream()
.mapToInt(Integer::intValue)
.reduce(
reductionOperation
).getAsInt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment