Download Object-Oriented Programming
Transcript
is like a data type in procedural languages, but much more powerful. Classes define the data that make up objects
and the messages that objects understand.
For example, a stack class (actually
called OrderedCollection) includes instance variables firstElement and lastElement and responds to messages such
as push and pop.
Instance variables are attributes of an
object and are like fields in a record
structure in C or Pascal. You can create
new classes and define methods using
the browser.
Rather than access the private instance
variables of a class directly using the dot
notation (e.g., stack.firstElement) you can
encapsulate the data by using messages
only. That way you reduce the dependencies on the implementation.
For example, if you need to get the
time required for an activity x, you
should send a getTime(x) message rather
than refer directly to the instance variable
x.time.
It doesn't matter if x is a Milestone, a
Task, a PERTTask or even a Project, as
long as it knows how to respond to a getTime message. This can make the critical
path recalculation algorithm easier to
write since you eliminate special cases for
Milestones; you just define its getTime
method to return zero. This results in significant savings in code.
Inheritance
What really makes classes powerful is
the use of inheritance. You can create
descendant classes that build on the
functionality already found in the system.
For example, the OrderedCollection
class mentioned earlier descends from
the Array class. Similarly, you can create
descendants of classes like Window and
Dialog to build the user interface to this
application without having to know the
more than 400 Microsoft Windows function calls. Descendant classes automatically include much of the generic windowing behavior that users expect in a
Windows application.
Inheritance lets us reuse code when
something is "a little different" from an
object that already exists. For example, I
defined the classes Network and Node to
provide generic network connection
capabilities. You can create a new network, connect some nodes to it, disconnect nodes and so on.
This may not be very exciting, but it
lets us factor out part of the application,
test it, and then forget about it. Since
20
MICRO CORNUCOPIA, #44, Nov-Dec 1988
Figure 4 - The Connect Method.
Public protocol :
connect(Nl, N2);
/* e.g. Nl -> N2 */
Private implementation:
/*Connect self->aNode by updating self's outputs and
aNode's inputs. Also, aNode should know it's network
and the position should be set. */
Def connect(self, aNode)
{ addinputs(aNode, self);
addOutputs(self, aNode);
setNetwork(aNode, network);
setPosn(aNode, self);
/*To connect node nl->n2, nl's outputs must contain n2. */
Def addOutputs(self, aNode)
{ add(outputs, aNode);
}
/*To connect nl->n2, n2's inputs must contain nl. */
Def addinputs(self, aNode)
{ add(inputs, aNode);
}
/***/
there's nothing inherently network or
node-like in the system already, these
classes descend directly from class Object.
Rather than keep track of the connections in the network, the nodes themselves maintain a set of input nodes and
output nodes as instance variables inputs
and outputs. The network, then, just
maintains the start and end nodes. The
network is actually implemented as a
doubly-linked list, but this is "private"
information and is encapsulated with
connect and disconnect methods.
You can implement the connect
method (see Figure 4) as addOutputs and
addinputs messages since both the nodes
must know about the connection that is
made. These two steps could easily have
been done in the connect method, but by
having each node manage its own instance variables, you can deal with networks made up of other networks.
The setPosn method is another example of private protocol. I use this
method to update the onscreen position
of the node and I wrote the disconnect
method in a similar fashion.
Interactive Tools
Once you've defined the network and
node classes, you can create a network
and examine it to make sure it's as you
expect. Actor encourages an interactive
programming style and has a rich set of
tools to make it easy to test and debug
code one piece at a time.
If any runtime errors show up, a
source code debugger will pop up
automatically. You can fix the code on
the fly and resume execution.
Any time you want to examine an object, you can call up an inspector and see
the values of the instance variables. You
can also inspect the instance variables of
an object from within another inspector
and trace through an entire network easily.
Actor also has a code profiler that you
can use to determine which methods will
give you the most optimization. You can
then selectively use early binding techniques to speed up critical code by
eliminating the message send overhead.
With the proper use of early binding in
critical areas, you can improve performance by 25 to 30%.
The Project Manager Classes
Once you're sure the generic Network
and Node classes are working properly,
you can define the Project, Activity, and
other classes listed in Figure 2.
The Project class will descend directly
from the Network class and include additional functionality related to the application. For example, you need to be
able to recalculate the critical path of the
project.
The Activity class is a formal class
that will group behavior common between its descendants Milestone and
Task. You won't ever create Activities
themselves (except for testing), instead
they'll always be either a Milestone or a
Task.
Alternatively, you could have had
Task descend from Milestone, but you'd