Re: OO Theory misconceptions (was Re: can I make a list of
Re: OO Theory misconceptions (was Re: can I make a list of
- Subject: Re: OO Theory misconceptions (was Re: can I make a list of
- From: email@hidden
- Date: Thu, 31 Jan 2002 23:20:51 -0500
On Thu, 31 Jan 2002 13:10:26 +0000, Nigel Garvey
<email@hidden> noted,
>
email@hidden wrote on Thu, 31 Jan 2002 00:02:31 -0500:
>
> on makeSillyObject from initialValue
[...]
>
> on makeSillyNumber from initialValue
>
> script
>
> property parent : makeSillyObject from initialValue
>
> to double()
>
> return 2 * my v
>
> end double
>
> to root()
>
> return sqrt(my v)
>
> end root
>
> end script
>
> end makeSillyNumber
>
Allowing for the fact that there's no sqrt() function provided, this
>
result depends on makeSillyNumber having been called with a parameter of
>
42.
Oops. sqrt() is a handler in the Satimage osax. (I had thought it was in
Standard Additions.) If you want it to run, just replace 'sqrt(my v)' with '(my
v)^ 0.5'
And yes, the result depends on makeSillyNumber being called with a parameter of
42. The sillyNumber object has data (its value) and operations on the data
(like returning its square root, or twice itself.) The examples are trivial
(which is why its a silly example), mostly because numbers are fairly
full-featured values themselves. But say these were complex numbers, or
quaternions, or matrices instead. Something like,
createMatrix from {{1,2,3},{4,5,6},{7,8,9}} returning A
determinant() of A
--> result: 0
eigensystem() of A
--> result: {{eigenvalue: 16.117, eigenvector: { 0.232, 0.525, 0.819},
{eigenvalue: -1.117, eigenvector: { 0.786, 0.087, -0.612},
{eigenvalue: 0, eigenvector: {-0.408, 0.816, -0.408}}
createMatrix from {{1,0,0}, {0,1,0}, {0,0,1}} returning B
tell A to multiply by B
--> result: <<script>> (another matrix)
-- or, you can say, "multiply of A by B"
I think this shows the power of objects when they encapsulate both the data and
the operations.
The fact that my sillyNumber was created with a value of 42 might cause
confusion or misgivings for two reasons. Confusion may be caused the
distinction between a class and an instance of the class. Misgivings may be
from the concern that each instance of the object it toting around all that
code. Well, don't be confused and don't worry.
When I define a constructor function, that looks like this:
on makeThing from somestuff
script
-- [...]
end script
end makeThing
That constructor function establishes the class of Things. Then when we call
the constructor,
makeThing from {stuff:42} returning myThing
myThing is an instance of a Thing. In this particular case, I made a
constructor that takes some initial values for the object it constructs; it's
also OK to make a constructor that generates identical objects, and then tell
each object how to set itself up.
on makeThing()
script
-- [...]
end script
end makeThing
makeThing() returning myThing
tell myThing to initialize from {stuff:42}
Now, if the Things initialize themselves, I could define the first one
statically, and then copy it to make additional ones. Like this
script myThing
-- [...]
end script
copy myThing to myNewThing
tell myThing to initialize from {stuff:42}
tell myNewThing to initialize from {stuff:666}
Each of these different approaches may be appropriate depending on the specific
application.
OK, that's the story with classes and instances. How about efficiency?
I did a quick test some time ago to see whether copying script objects took a
lot of storage, and found that no, it didn't. I don't remember the exact
storage requirements, but when I took a very large script object, with a few
properties and many large handlers, copies of the script object were much
smaller than the first one. Apparently, AppleScript does the right thing,
copying the properties but sharing the handlers. Each copy of the script object
gets its own set of properties, and you can change one script object's property
without affecting another, but since you can't alter the handlers, they are
shared.
The only thing I've found that is a real efficiency bust is passing script
objects from one script application to another. The full object gets passed
each time. And the Event Log shows nothing but binary gobbledygook.
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden