home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Education
/
collectionofeducationcarat1997.iso
/
COMPUSCI
/
TOT11.ZIP
/
TOTDOC11.ZIP
/
CHAPT3.TXT
< prev
next >
Wrap
Text File
|
1991-02-11
|
49KB
|
1,059 lines
Toolkit Basics
"No, what I said was 'No new taxis'" Georgio Bush, 1990
An Object Primer
Let's talk cars. We all know that you don't need to be a combustion
mechanic to drive a car (that's a veeehickle if you're from Texas), but
most good drivers know a few basic principles about how a car works.
The same philosophy is true for object oriented programming: you don't
need to be an OOP guru to use the Toolkit but a little understanding is
helpful. The modest goal of this section is to explain some of the
object oriented principles to help you get up and running with the
Toolkit as soon as possible. Over time, you will gain OOP expertise
without even trying! If you are already familiar with OOP, skip to the
next section.
For now, we will concentrate on those aspects of OOP you will need in
order to understand how to use the Toolkit. In Part 2: Extending the
Toolkit, the more advanced concepts of inheritance and extensibility
are addressed.
In plain Pascal, there are types like integer, real, string, etc. and
you declare variables to be of a specific type. You can even define new
types. For example, the following declaration defines a new type
Address:
TYPE
Address = record
Name: string[20];
Street: string[30];
CityStZip: string[20];
end;
Having defined the type Address, you can then create variables of this
type, e.g.
VAR
BobsHouse: Address;
In most programs, you will write procedures and functions that can
manipulate the data stored in these variables, e.g.
procedure PrintLabel(Addr: address);
function Zip(Addr:address): string;
3-2 User's Guide
--------------------------------------------------------------------------------
But most of you know this stuff anyway. Things aren't so very different
with OOP. The primary difference is that, with OOP, the data and the
procedures and functions which manipulate the data are combined into a
single type, called an object. This concept of combining data and
procedures/functions is referred to as encapsulation. Listed below is a
way to create an object type called AddressOBJ:
TYPE
AddressOBJ = object
Name: string[20];
Street: string[30];
CityStZip: string[20];
{methods ...}
procedure PrintLabel;
function Zip: string;
end; {object}
VAR
BobsHouse: AddressOBJ;
Notice that the object type declaration is very similar to a record
declaration, but the keyword object is used instead of record. The
procedure and function declaration are included in the object declara-
tion, and are referred to (in OOP nomenclature) as methods. The methods
are not passed the address record as a parameter, because the procedure
and functions in an object can directly access the data in the object,
and so don't need to be passed the data. The "variable" BobsHouse,
which is of type AddressOBJ, is referred to as an instance - in other
words, object variables are called instances! All object type declara-
tions in the Toolkit end with the characters "OBJ".
The individual elements of an object (just like a record) can be
accessed by using the "dot" notation. For example, to print a label,
you would use the following statement:
BobsHouse.PrintLabel
You can also use the "with identifier do" shortcut, e.g.
with BobsHouse do
begin
PrintLabel
.....
end; {with}
In theory, you could also access the data elements using the dot
method, e.g. BobsHouse.Street := '12 Paradise Drive', but you
shouldn't! While it is syntactically correct and will compile, this is
bad practice. A basic precept of encapsulation is that you never access
Toolkit Basics 3-3
--------------------------------------------------------------------------------
the data elements of an instance directly. You should create methods
within the object to access the object's data. In the above example,
there is no way to access the data in the object (other than breaking
the rules) so a data update method would need to be created. Listed
below is an improved type declaration of the AddressOBJ:
TYPE
AddressOBJ = object
Name: string[20];
Street: string[30];
CityStZip: string[20];
{methods ...}
procedure Adddata(Nam,St,Ci,St,Zp:string);
procedure PrintLabel;
function Zip: string;
end; {object}
The new AddData method would be used to set the values of the Name,
Street and CityStZip elements. For example, the following two state-
ments would print the label:
with BobsHouse do
begin
AddData('Bobbo','12 Paradise Drive',
'Dome','TX','77186');
Printlabel;
end;
If you are curious, each method identified in an object must be
included in the body of the program or unit. For example, later in the
program there might be the following method definition:
...
procedure AddressOBJ.Adddata(Nam,St,Ci,St,Zp:string);
{}
begin
Name := copy(Nam,1,20);
Street := copy(St,1,30);
...
end; {of proc}
...
Now, back to the main discussion. The advantage of using methods to
access the object data is that the object user (that's you) does not
need to know or care about how the data is stored within the object.
The internal data structure might be modified at some stage to use
3-4 User's Guide
--------------------------------------------------------------------------------
pointers or to store ASCIIZ strings, etc, but the same method AddData
can still be called in the same way as previously. What Adddata does
(behind the scenes) may have changed, but your program still compiles
and runs without modification.
For various reasons, many objects need to be initialized before they
can be used. For example, some memory may need to be allocated on the
heap. When you have finished with such an object, the memory will need
to be disposed of. Just because the procedure in which you declared an
object instance (i.e. variable) has terminated, it does not necessarily
mean that the object data has been removed.
Although not mandatory, a de facto OOP standard is to have two special
methods for all objects. Namely, INIT and DONE. These special methods
are used to initialize an object and dispose of an object, respec-
tively. Throughout the Toolkit, every object has IN