More Related Content Similar to XpUg Coding Dojo: KataYahtzee in Ocp way (20) More from Giordano Scalzo (12) XpUg Coding Dojo: KataYahtzee in Ocp way8. Then write a factory that returns an object,
or an aggregate of objects, that make the
test pass.
9. The factory should be limited to
creating objects and linking them
together. No conditionals allowed.
11. 2. Can you make it pass by changing the
factory and/or creating a new class and
nothing else?
14. The refactoring should bring the
system to a state where it’s possible
to implement the next test just by
changing the aggregate of objects
that is returned by the factory.
15. Be careful not to implement new
functionality; the current test should
still fail.
17. package xpug;
import junit.framework.Assert;
import org.junit.Test;
public class AFizzbuzzer {
@Test
public void shouldReturnSameNumberForPlainNumber() {
Fizzbuzzer fizzbuzzer = FizzbuzzerFactory.create();
Assert.assertEquals("1", fizzbuzzer.say(1));
Assert.assertEquals("2", fizzbuzzer.say(2));
}
}
20. @Test
public void shouldReturnFizzForMultipleOfThree(){
Fizzbuzzer fizzbuzzer = FizzbuzzerFactory.create();
Assert.assertEquals("Fizz", fizzbuzzer.say(3));
Assert.assertEquals("Fizz", fizzbuzzer.say(6));
Assert.assertEquals("Fizz", fizzbuzzer.say(9));
}
22. package xpug;
public class Echo implements Counter {
@Override
public boolean canHandle(int digit) {
return true;
}
@Override
public String say(int digit) {
return String.valueOf(digit);
}
}
23. package xpug;
import java.util.List;
public class Fizzbuzzer {
private List<? extends Counter> counters;
public Fizzbuzzer(List<? extends Counter> counters) {
this.counters = counters;
}
public String say(int digit) {
for (Counter counter : counters)
if (counter.canHandle(digit))
return counter.say(digit);
return "";
}
}
25. @Test
public void shouldReturnFizzForMultipleOfThree(){
Fizzbuzzer fizzbuzzer = FizzbuzzerFactory.create();
Assert.assertEquals("Fizz", fizzbuzzer.say(3));
Assert.assertEquals("Fizz", fizzbuzzer.say(6));
Assert.assertEquals("Fizz", fizzbuzzer.say(9));
}
26. package xpug;
public class Fizz implements Counter {
@Override
public boolean canHandle(int digit) {
return digit % 3 == 0;
}
@Override
public String say(int digit) {
return "Fizz";
}
}