|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Director Don't Squander Your Inheritance
Just get the job done - right
By: Kerry Thompson
Nov. 18, 2004 12:00 AM
"Hi, my name is Kerry, and I'm a programmer." I looked around the room as people murmured "Hello Kerry." Most of them were what you would expect - some shaggy long hairs, a couple of women with buzz cuts, a pony tail here and there, some middle-aged guys with even less hair than I have. A lot of the people were generic American white, but there was a Chinese couple, a clean-cut man with a yarmulke, a couple of guys who looked Indian, and one lone, sad-looking black woman. Most of them tended toward middle age, probably because most of the youngsters are still out there chasing the next bug-fix high, or on deadline binges. I took a deep breath and continued. "I've done it all, pretty much. FORTRAN, COBOL, Turbo Pascal, Assembler, RPG, Lingo, JavaScript, XSLT, Lingo, ActionScript, HTML..." People were nodding, thinking of their own lost years. "Basic..." I could see a couple of people purse their lips and shake their heads, but I pushed ahead. I wanted to come clean. "...and, in a lot of programs, I use inheritance." There was a stunned silence. Apparently, some things were too much, even for the hard-core programmers here tonight. After a moment, the moderator said, "Emm, yes, inheritance." I could hear the Dublin accent in his voice. "Right, then." He collected himself. "What drove you to that?" I shrugged my shoulders. "I told myself it was productivity. In truth, I think I had been doing Lingo so long, I just didn't get the rush any more. So I had to do more programs. To do more programs, I had to do OOP and, inevitably, inheritance." One of the Indian guys spoke up. "Didn't your boss say anything?" "I'm a freelancer. My clients never knew. They just saw me whipping out the programs in record time. They loved it, and never asked. They became enablers, sending me more and more work." I paused. "And now I'm here." The Backslider "Pretty much," I said. "I thought it would be hard, but once I started, well, it was like a curtain parted and the secrets of the universe were revealed." A bit of hyperbole, maybe, but it's hard to put a programming high into words. Pony Tail looked over his shoulder again, and I could see a group of people watching us. He leaned closer to me and whispered, "I've got a laptop in the car." He nodded at the group. "Maybe you could show us some of this inheritance stuff." I must have looked dubious, because he quickly added, "You know, just so we'd know what it's like." I knew I shouldn't, but I nodded. Okay. Pony Tail looked at the group, larger now, jerked his head at the door, and started walking out. I followed, and, by ones and twos, people from inside drifted out onto the sidewalk. Pony Tail disappeared into his car for a moment, and then emerged with a Dell laptop. "Pentium," he said proudly as he cracked it open and the glow from the LCD lit the faces around us. "Two-point-eight, a gig of RAM, 80 gigs..." "Hey, put that away!" We all looked at the shaggy, bearish man who had spoken. He glanced at the door to the room we had all come from. The moderator was in earnest conversation with one of the buzz-cut women. As far as I could tell, the room was empty except for them. It wouldn't be long before the moderator noticed the gaggle on the sidewalk. "My place is just across the street," The Bear said urgently. "Dual processor G-5, Cinema display, XP 2004." The Bear's Den "Programming by difference," I said, somewhat self-importantly, but they were listening. I stuck a CD in the drive. "Not my phrase. Irv Kalb taught me that." A murmur went through the room. Everybody had heard of the legendary Irv Kalb, but no one here except me had actually met him. "That was Irv's thinking when he used ancestors. With a group of related objects, he'd build one base class with all the common stuff, and build separate objects that have the differences." "You actually know Irv?" the Chinese man asked. "Yeah, sure. Look, here's a sprite manager he wrote. I use it all the time." I opened a movie on the CD I had put in, double-clicked a cast member, and the code appeared on the screen (see Listing 1). I heard a couple of soft whistles. "Elegant," somebody said. "I could use that in my programs." A pause, then the invietable. "All of my programs!" A murmur of assent went around the room as people realized the implications of what I was showing them. "Hold on," I said. "We're getting ahead of ourselves. You have to use this stuff right, or you can get in real trouble. Look - see that icon? That means it's a parent script. Ya gotta do that." "Why?" somebody asked. "What happens if you don't?" "No telling," I shot back. "I know a guy who spent three days trying to debug some code. It turned out the code was okay, but he had it in a movie script. Try to instantiate an object from a movie script, boom! There goes your schedule, your deadline, your bonus." People were nodding. They had all been there, one way or another. "Okay, basics," I said. "I do a lot of educational stuff, so here's one way I use inheritance. Say I want to teach Spanish, or maybe math, or reading. I'm going to have a lot of activities - fill in the blank, drag-and-drop, stuff like that. "They're all different activities, but they all have a lot in common. I make them frame scripts, so they all have to handle stepFrame, and maybe begin- Sprite and endSprite." I started typing. "Here's the frame script for the first activity."
property ancestor
on new me
ancestor = script ("Activity parent").new()
"All you have to do is declare an ancestor property, then assign a script to it. That makes the 'Activity parent' script part of your object. I'll create that in a second, but first I'll name this one 'Activity Script 1.'" I named it, used the property inspector to make it a parent script, and saved the movie (I do that a lot). "Here's the ancestor. It's pretty simple." I clicked the '+' on the script window to create a new parent script, named it 'Activity parent', and typed in a short handler. on exitFrame go the frame end "That's the activity parent, the one you declared in the activity script. I'll name it 'Activity parent', since that's what the activity script is expecting to use as an ancestor. "Now watch this." I copied and pasted the "Activity Script 1" cast member, and renamed the copy to "Activity Script 2". "Now you have a start on two activities. They both use the ancestor 'Activity Parent'." "So, you've made two objects," somebody said from the back of the group. You've saved, what, three lines of typing? What's the big deal?" "Fair enough," I said. "Only three lines. But what if you have fifteen activities? You would save nearly fifty lines of typing. Plus, you can do this." I opened the "Activity Parent" script and changed it to this: on exitFrame go the frame end on beginSprite -- do your initialization here end on endSprite -- do your cleanup here end "Imagine what it would be like if your beginSprite and endSprite routines were 10 or 15 lines long. You would save a few hundred lines of typing. For me, that's a few hundred lines of potential typos. Plus, you'd have everything in one place for any changes. "I know what's coming next, too. What if your activities have their own initialization and cleanup routines, and there are differences from activity to activity? "I go back to what Irv said - 'program for differences. ' Let's change the activity parent." I hit the keyboard again, and a few seconds later had Listing 2 in "Activity Script 1". "See, you can override the ancestor's methods. You can even override the method, and still call the ancestor's code. That way you can do activity-specific stuff in the activity code, and use the ancestor's code for things that are common to all the activities. "Here's another example. You want your activities to handle and filter keystrokes. At the same time, you want to have standard keystrokes, stuff that doesn't change from activity to activity. Navigation, for example. Let's say you're going to use a keystroke to cycle through the activities - maybe F1." "That's reserved. Brings up help." "Oh, yeah. Okay, how about Ctrl-F1? Director doesn't use that, does it?" Shrugs and blank looks. Nobody knows all the Director shortcuts. "We'll try it. "While we're at it, we need a couple of activities that use different keystokes. Let's make Activity 1 a simple text editor, and in Activity 2 we'll move a ball around the screen with arrow keys. Simple stuff, but it's good enough to give you the idea. And anyway, we don't want to be here all night, do we?" I grinned. I knew most of them would happily spend all night programming. We'd gripe about it, but mostly for effect (and to try to get a raise). "So, we need to change the Activity parents." I brought up the code for activity 1 and made a few quick changes.
property pKeyHandler
property ancestor
on new me
ancestor = script ("Activity parent").new()
pKeyHandler = script("Activity 1 Key Handler").new(me)
end
Similar changes went into the Activity 2 code. Then I clicked the "+" sign again to create a new script, and named it "Activity 1 Key Handler". This one took me a little longer, but within a couple of minutes, with some help from the group, I had the code in Listing 3. I made a copy of the script and called it "Activity 2 Key Handler". I went in and changed it to look for arrow keys:
on mDoKey me, keyID
case keyID of
28, 29, 30, 31: -- arrow keys
pOwnerActivity.mActOnKey(keyID)
otherwise -- send the keyDown to my ancestor.
ancestor.mDoKey(keyID)
end case
end
"So, now we need to handle the keys in the activities. I'll just add Listing 4 to 'Activity Script 1'; and Listing 5 to 'Activity Script 2'. "You guys all know how to make a text sprite and put it on screen, don't you?" Nods all around. "And a ball to move around? I'll leave the initialization of those up to you. That's the activity-specific keys. Now we just need to create a key handler parent to handle Ctrl-F1. That's a little more complicated, but I have one on this CD here." I rummaged about in my collection of scripts and found the one I was looking for. With a few changes, we soon had a new script called "Key Handler Parent"(see Listing 6). "And here's a bonus - the ancestor with the function keys defined, the 'Key Definitions' ancestor used in the key handler parent." (See Listing 7.) I swiveled around in the Bear's Aeron chair. "That's about it. Any questions?" There was a silence for a moment, then, from the back of the group, "Can we have a copy," in our moderator's unmistakable Dublin accent. Looking sheepish, the Bear handed me a spindle of CD-ROMs. I handed a printout to the group and got to work on burning 20 CDs. Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||