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
import spock.lang.Specification | |
class CalculatorTest extends Specification { | |
//All tests in each package do the same thing | |
//But I wanted to keep each step refactor for academic purposes | |
//along its tests | |
def final calculator = new Calculator() | |
static final SUM = '+' |
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
package behaviouralSwitch.sentence; | |
public class Calculator { | |
public int calculate(char op, int n1, int n2) { | |
int result; | |
switch(op) { | |
case '+': |
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
package behaviouralSwitch.structureAlternative; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.function.BinaryOperator; | |
public class DataStructureCalculator { | |
private static Map<Character, BinaryOperator<Integer>> operations = | |
new HashMap<Character, BinaryOperator<Integer>>() {{ |
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
public class EnumCalculator { | |
//Notice how concise the code has become in the logic | |
public int calculate(char op, int a, int b) { | |
return OperationEnum.of(op).operate(a,b); | |
} | |
} |
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
//Is common to use all matching functions. Whichever you need to check if a list of flags are set, | |
//if some conditions meet, even if all the fields are equal in a equals function, you need to do an all match. | |
//Many people would do this, but, please, do not do this. Specially if the set of conditions is large. | |
if (conditionA()) { | |
if (conditionB()) { | |
if (conditionC()) { | |
return true; | |
} |
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
import java.util.List; | |
interface Operation { | |
boolean isOperation (String input); | |
int operate (List<Integer> numbers); | |
} |
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
class OperationInterfaceAsClass { | |
Predicate<String> isOperation; | |
Function<List<Integer>, Integer> operate; | |
private OperationInterfaceAsClass(){}; | |
public static OperationInterfaceAsClass of( | |
Predicate<String> isOperation, | |
Function<List<Integer>, Integer> operate |
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
class OperationInterfaceAsClass implements Operation { | |
Predicate<String> isOperation; | |
Function<List<Integer>, Integer> operate; | |
private OperationInterfaceAsClass(){}; | |
public static OperationInterfaceAsClass of( | |
Predicate<String> isOperation, | |
Function<List<Integer>, Integer> operate |
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
class OperationAbstractAsClass implements Operation { | |
Predicate<String> isOperation; | |
IntBinaryOperator reductionOperation; | |
private OperationAbstractAsClass(){}; | |
public static OperationAbstractAsClass of( | |
Predicate<String> isOperation, | |
IntBinaryOperator reductionOperation |
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
class CharacterData { | |
int HP; | |
int attack; | |
int defense; | |
//Implement all the boilerplate :D | |
} |
OlderNewer