May 19, 2006
The Craftsman: isTooHotForMe!Jasmine rolled her eyes and jabbed Jerry in the ribs with her elbow. "You've got too much color on your brain," she said with a smile that seemed to last just a little too long.
I shook my head and said, "That method might be better off in the enumeration itself. Look."
public enum RGB {
red(0xff, 0x00, 0x00),
green(0x00, 0xff, 0x00),
blue(0x00, 0x00, 0xff),
white(0xff,0xff,0xff);
public final int r;
public final int g;
public final int b;
RGB(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
public boolean isBlue(){
return b > 2*(r + g);
} }
"Does that work?" Avery demanded.
"Sure," I said, and wrote a test to prove it.
public class RGBTest extends TestCase {
public void testIsBlue() throws Exception {
assertTrue(RGB.blue.isBlue());
assertFalse(RGB.red.isBlue());
assertFalse(RGB.green.isBlue());
} }
"Wow," said Jerry and Jasmine in unison. They looked at each other and giggled.
"So we could do the same thing to the Taco.Salsa enum too, right?" asked Avery. "Let me try."
enum Color {
red, green, burntumber}
enum Salsa {
mild(10, Color.red),
medium(1000, Color.green),
hot(100000, Color.burntumber);
public final int scovilles;
public final Color color;
Salsa(int scovilles, Color color) {
this.scovilles = scovilles;
this.color = color;
}
public boolean isTooHotForMe() {
return scovilles > 30000;
} }
"And here are the tests," he continued.
public class TacoTest extends TestCase {
public void testIsTooHot() throws Exception {
assertTrue(Taco.Salsa.hot.isTooHotForMe());
assertFalse(Taco.Salsa.medium.isTooHotForMe());
assertFalse(Taco.Salsa.mild.isTooHotForMe());
} }
Avery grinned as he stared at the green test indicator bar on the wall. "That's really cool," he finally said.
"Yes," I agreed. "But it's better than that. Those methods can be polymorphic."
"Wow! " said Jerry and Jasmine ... again in unison ... again giggling at themselves.
Avery and I rolled our eyes. "Yeah. Look at this," I said.
public class Taco {
enum Color {
red, green, burntumber}
enum Salsa {
mild(10, Color.red) {
public int sips() {
return 0;
}
},
medium(1000, Color.green){
public int sips() {
return 1;
}
},
hot(100000, Color.burntumber){
public int sips() {
return 5;
}
};
public final int scovilles;
public final Color color;
public abstract int sips();
Salsa(int scovilles, Color color) {
this.scovilles = scovilles;
this.color = color;
}
public boolean isTooHotForMe() {
return scovilles > 30000;
} } }
"Does that work?!" Avery demanded once again.
Everyone looked at him for a second, and then started to laugh.
After a few seconds, even Avery laughed. "Sure," I said, " here are the tests."
public void testSips() throws Exception {
assertEquals(0,Taco.Salsa.mild.sips());
assertEquals(1, Taco.Salsa.medium.sips());
assertEquals(5, Taco.Salsa.hot.sips());
}
What happened next, I can only describe in code:
wow() * 3; giggle() * 3; eye_rolls() * 4;
"OK," Jerry said with some finality. "This has gotten out of control. I think it's time we got back to work." Everyone headed for the door. "But wait!" I said. "I still haven't asked my question."
"OK, hotshot," Jasmine responded. "Hurry it up. What's your question?"
I looked at the three of them. "What would you use this for?"
|
|
||||||||||||||||||||||||||||||
|
|
|
|