• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
XpUg Coding Dojo: KataYahtzee in Ocp way
 

XpUg Coding Dojo: KataYahtzee in Ocp way

on

  • 4,891 views

Introduction to Ocp way to do a kata, used during a Xpug Milano Coding Dojo.

Introduction to Ocp way to do a kata, used during a Xpug Milano Coding Dojo.

Statistics

Views

Total Views
4,891
Views on SlideShare
4,545
Embed Views
346

Actions

Likes
4
Downloads
55
Comments
2

7 Embeds 346

http://giordano.scalzo.biz 329
http://www.slideshare.net 9
http://www.linkedin.com 4
http://www.geekagenda.it 1
http://www.scalzo.biz 1
http://translate.googleusercontent.com 1
http://www.netvibes.com 1
More...

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

XpUg Coding Dojo: KataYahtzee in Ocp way XpUg Coding Dojo: KataYahtzee in Ocp way Presentation Transcript

  • Xpug Coding Dojo: The OCP Kata
  • Kata
  • Pairs
  • Randori
  • Costraints
  • The OCP Kata http://matteo.vaccari.name/blog/archives/293
  • 0. Write the first failing test.
  • Then write a factory that returns an object, or an aggregate of objects, that make the test pass.
  • The factory should be limited to creating objects and linking them together. No conditionals allowed.
  • 1. Write the next failing test.
  • 2. Can you make it pass by changing the factory and/or creating a new class and nothing else?
  • If yes, great! Go back to 1.
  • If not, refactor until you can.
  • 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.
  • Be careful not to implement new functionality; the current test should still fail.
  • An example
  • 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)); } }
  • package xpug; public class FizzbuzzerFactory { public static Fizzbuzzer create() { return new Fizzbuzzer(); } }
  • package xpug; public class Fizzbuzzer { public String say(int digit){ return String.valueOf(digit); } }
  • @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)); }
  • package xpug; public interface Counter { public boolean canHandle(int digit); public String say(int digit); }
  • 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); } }
  • 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 ""; } }
  • package xpug; import java.util.Arrays; public class FizzbuzzerFactory { public static Fizzbuzzer create() { return new Fizzbuzzer(Arrays.asList( new Echo())); } }
  • @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)); }
  • 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"; } }
  • package xpug; import java.util.Arrays; public class FizzbuzzerFactory { public static Fizzbuzzer create() { return new Fizzbuzzer(Arrays.asList( new Fizz(), new Echo())); } }
  • ... ... ... ... ... ... ...
  • package xpug; import java.util.Arrays; public class FizzbuzzerFactory { public static Fizzbuzzer create() { return new Fizzbuzzer(Arrays.asList( new Fizzbuzz(), new Buzz(), new Fizz(), new Echo())); } }
  • KataYahtzee
  • Let’s start!