Home (Spotlight) | TOC
| Products & Ordering | Technical
Info | Contact Zeus
Zeus Tech Note
Object-Oriented Programming in Lingo
Copyright © 1997.
Zeus Productions. All Rights Reserved.
Written by Bruce A. Epstein
Procedural vs. Object-Oriented Programming:
Typical Lingo scripting is procedural because you create procedures
(or functions) to perform a particular task. A procedure typically performs
a single operation. For example, a "multiply" procedure might
multiply two numbers:
on multiply a, b
return (a*b)
end
To implement a "stop watch" using a procedural approach, we could
use global variables to communicate between the various functions, as below
(Note: this example is heavily simplified and not very robust).
-- Reset the timer to zero
on resetTimer
global gCurrentTime
set gCurrentTime = 0
end resetTimer
-- Start the timer running
on runTimer
global gStartTime
set gStartTime = the ticks
end runTimer
-- Stop the timer (assumes timer has been reset and started)
on stopTimer
global gCurrentTime, gStartTime
set gCurrentTime = (the ticks - gStartTime)
end stopTimer
-- Report the timer's value (assumes timer has been stopped)
on reportTimer
global gCurrentTime
return (gCurrentTime/60.0)
end reportTimer
We could use our timer to check how fast Lingo can perform 1000 iterations
of a repeat loop:
-- Test the timer functions
on testTimer
resetTimer
runTimer
repeat with x = 1 to 1000
nothing
end repeat
stopTimer
put reportTimer()
end testTimer
To implement two timers that operate simultaneously using the procedural
approach, we would need to create additional global variables and handlers
to avoid conflicts between the two timers.
Object-oriented programming (OOP) is an ideal alternative. (Lingo
allows both procedural and object-oriented programming in the same file,
so you can mix and match as needed). We can build a timer object,
and create multiple instances of it, each of which operates independently.
Each instance can maintain it's own properties, which are "global-like"
variables that are private to the object. Properties can be used by any
handler or method within the object, and persist over time, but they
are not generally visible outside of the object (see the property
keyword for exceptions), which reduces potential conflicts.
Objects contain on or more methods to perform the desired functions.
For example, a "Timer" object might behave like a stop-watch with
four buttons, each represented by a method:
- Method 1 - Reset the timer to zero
- Method 2 - Start the timer
- Method 3 - Stop the timer
- Method 4 - Report the timer's current time in seconds
To create a child object:
- Write the Lingo methods for the object.
- Place the Lingo in a Parent Script. (Create a Parent Script
by changing the script type in the Script cast member info dialog box to
"Parent").
- Create an instance (child object) using the "
new
"
command.
- Use the instance handle returned by the
new
method
to call the other methods.
Here is an object-oriented version of the previous example. This Lingo should
be placed in a Parent Script cast member named "Timer".
-- Cast Member "Timer"
property pCurrentTime, pStartTime
on new me
return me
end
-- Reset the timer to zero
on resetTimer me
set pCurrentTime = 0
end resetTimer
-- Start the timer running
on runTimer me
set pStartTime = the ticks
end runTimer
-- Stop the timer (assumes timer has been reset and started)
on stopTimer me
set pCurrentTime = (the ticks - pStartTime)
end stopTimer
-- Report the timer's value (assumes timer has been stopped)
on reportTimer me
return (pCurrentTime/60.0)
end reportTimer
Notes about the object-oriented version of our timer:
- We added a method called "
new
", which is used
to instantiate the object.
- The variable "
me
" is used by the calling script
to identify each instance of the child object.
- Property variables have replaced the global variables used in the
procedural example. The properties can contain a different value for each
instance of the object (each timer) and are declared with the keyword "
property
".
(Child objects can use global variables, but do not necessarily do so. Such
globals would be accessible to all scripts, and could each hold only one
shared value. Values specific to each instance should be declared as properties
instead.)
The testTimerObj
handler can be placed in a Movie Script and
used to test the Timer object. Note that there are two separate instances
of the timer (timer1
and timer2
) running at once.
-- Test the Timer child object
on testTimerObj
set timer1 = new (script "Timer")
set timer2 = new (script "Timer")
resetTimer (timer1)
runTimer (timer1)
put "Testing the speed of the repeat loop"
repeat with x = 1 to 10
resetTimer (timer2)
runTimer (timer2)
repeat with y = 1 to 5000
nothing
end repeat
stopTimer (timer2)
put "5000 Iterations took" && reportTimer(timer2) && "seconds"
end repeat
stopTimer (timer1)
put "10 tests took a total of" && reportTimer(timer1) && "seconds"
set timer1 = 0
set timer2 = 0
end testTimerObj
Child Objects are generally a good idea when you want to create:
- Multiple items with similar behaviors or properties, such as
timers, bouncing balls or space aliens. Each child object (instance) can
maintain its own properties; so, although they are all intrinsically similar,
they can have significantly different manifestations of those properties.
For example, a ball's properties may include speed, acceleration, diameter,
mass, elasticity and color.
- Objects which have a state (a series of properties which vary
over time) such as a timer. The object can remember its own state, and is
therefore much easier to use and manipulate.
- Code that is independent of other code. Objects are encapsulated
so they can be developed separately by multiple programmers. Objects can
be insulated from the outside world, with the only interaction occurring
via the defined methods. This can simplify development and maintenance of
complex projects.
- Classes of objects in a hierarchy. Objects can inherit behaviors
from ancestors, allowing you to create a tree of related objects.
For example, a family tree of mammals, birds and reptiles may share common
ancestors, yet diverge in their specific behaviors or characteristics.
- Objects which persist over time or which require multiple operations.
For example, the FileIO Xtra is an object written in C. It allows you to
open, read, write, delete, search and close external files. The instance
keeps track of it's own properties, including the file's name and location
and the last position read within the file.
... more to come....
Home (Spotlight) | Table
of Contents | Links | Contact
Info
Place an Order | Products
for Sale | Licensing | Downloads
TechNotes | FAQs
| E-Mail Zeus | GuestBook
| Glossary
Copyright © 1996-1997. Zeus
Productions. All Rights Reserved.
(This document last revised August 1, 1997)