Chapter 2 A Trip to Objectville
I was told there would be objects. In chapter 1, we put all of our code in the main() method. Thats not exactly object-oriented. In fact, thats not object-oriented at all. Well, we did use a few objects, like the String arrays for the Phrase-O-Matic, but we didnt actually develop any of our own object types. So now weve got to leave that procedural world behind, get the heck out of main (), and start making some objects of our own. Well look at what makes object-oriented (OO) development in Java so much fun. Well look at the difference between a class and an object. Well look at how objects can give you a better life (at least the programming part of your life. Not much we can do about your fashion sense). Warning: once you get to Objectville, you might never go back. Send us a postcard.
Chair Wars
(or How Objects Can Change Your Life)
Once upon a time in a software shop, two programmers were given the same spec and told to "build it". The Really Annoying Project Manager forced the two coders to compete, by promising that whoever delivers first gets one of those cool Aeron chairs all the Silicon Valley guys have. Larry, the procedural programmer, and Brad, the OO guy, both knew this would be a piece of cake.
Larry, sitting in his cube, thought to himself, "What are the things this program has to do? What procedures do we need? And he answered himself, "rotate and playSound." So off he went to build the procedures. After all, what is a program if not a pile of procedures?
Brad, meanwhile, kicked back at the café and thought to himself, "What are the things in this program... who are the key players?" He first thought of The Shapes. Of course, there were other objects he thought of like the User, the Sound, and the Clicking event. But he already had a library of code for those pieces, so he focused on building Shapes. Read on to see how Brad and Larry built their programs, and for the answer to your burning question, "So, who got the Aeron?
In Larrys cube
As he had done a gazillion times before, Larry set about writing his Important Procedures. He wrote rotate and playSound in no time.
rotate(shapeNum) {
// make the shape rotate 360º
}
playSound(shapeNum) {
// use shapeNum to lookup which
// AIF sound to play, and play it
At Brads laptop at the cafe
Brad wrote a class for each of the three shapes
Larry thought hed nailed it. He could almost feel the rolled steel of the Aeron beneath his...
But wait! Theres been a spec change.
"OK, technically you were fi rst, Larry," said the Manager, "but we have to add just one tiny thing to the program. Itll be no problem for crack programmers like you two."
"If I had a dime for every time Ive heard that one", thought Larry, knowing that specchange-no-problem was a fantasy. "And yet Brad looks strangely serene. Whats up with that?" Still, Larry held tight to his core belief that the OO way, while cute, was just slow. And that if you wanted to change his mind, youd have to pry it from his cold, dead, carpal-tunnelled hands
Back in Larrys cube
The rotate procedure would still work; the code used a lookup table to match a shapeNum to an actual shape graphic. But playSound would have to change. And what the heck is a .hif fi le?
playSound(shapeNum) {
// if the shape is not an amoeba,
// use shapeNum to lookup which
// AIF sound to play, and play it
// else
// play amoeba .hif sound
}
It turned out not to be such a big deal, but it still made him queasy to touch previously-tested code. Of all people, he should know that no matter what the project manager says, the spec always changes.
At Brads laptop at the beach
Brad smiled, sipped his margarita, and wrote one new class. Sometimes the thing he loved most about OO was that he didnt have to touch code hed already tested and delivered. "Flexibility, extensibility,..." he mused, reflecting on the benefits of OO
Larry snuck in just moments ahead of Brad.
(Hah! So much for that foofy OO nonsense). But the smirk on Larrys face melted when the Really Annoying Project Manager said (with that tone of disappointment), "Oh, no, thats not how the amoeba is supposed to rotate..."
Turns out, both programmers had written their rotate code like this:
1) determine the rectangle that surrounds the shape
2) calculate the center of that rectangle, and rotate the shape around that point.
But the amoeba shape was supposed to rotate around a point on one end, like a clock hand.
"Im toast." thought Larry, visualizing charred Wonderbread. "Although, hmmmm. I could just add another if/else to the rotate procedure, and then just hard-code the rotation point code for the amoeba. That probably wont break anything." But the little voice at the back of his head said, "Big Mistake. Do you honestly think the spec wont change again?"