The past five years have seen a huge increase in the use of object-oriented technology. Programming
languages such as C++ and Objective C are now becoming relatively commonplace, object-oriented
databases are starting to become efficient enough to use on commercial projects and every degree
course in computing contains at least one course which focuses on object technology. Unfortunately
most of the teaching literature on object-orientation tends to be quite difficult to access; since Java is
an object-oriented programming language we have decided not to dive straight into a full description of the language, but briefly introduce you to some of the main concepts of object-orientation. In order to do this a number of examples of computer-based systems will be described and an object
view taken of their architecture. In describing these systems we will introduce some very small
fragments of Java code.
Some examples of object-based systems
This section will describe a number of computer applications and show how each can be regarded as consisting of a collection of objects and an outside world where the system executes via the process of message passing. The stored data will be necessary for the functioning of the system in which the objects are embedded In the example of the air traffic control system there is potentially a large amount of data that can be associated with a flight. Since we are only considering a very simple system we shall assume that only four items of data are associated with each object: the plane’s x coordinate position, y coordinate position, z coordinate position and the type of plane. These four items of data are important for the functioning of an air traffic control system; indeed the first three items are mandatory as it would be impossible to do very much in such a system without knowing where the planes are. The final piece of data is required in order to assign a runway on which the plane is to land as the airport may have a number of runways suited to different types of plane. The collection of data associated with an object is known as its state.
The shaded part represents the air traffic control system, while the unshaded part represents the outside world: the world of real aircraft, human controllers and concrete airports. Events occur in this outside world which affect the object world. A typical event might be a radar informing the system that a particular plane has moved its position or a particular plane has disappeared because it has removed itself from the area of the skies administered by the air traffic control system. These events will access or affect the object world within the computer – normally this is manifested in the destruction of existing objects, the construction of a new object, the updating of an object’s state or the retrieval of data from the object world. Let us look at some of these events and how they affect the simple air traffic control system:
• A plane appears in the air space controlled by the system. This results in a new plane object being
created and given values for its state.
• A plane disappears from the air space. This can occur for a number of reasons: the plane may
have left the air space during its onward journey, it could have landed or, very rarely, it could
have crashed. This results in the disappearance of the plane object.
• The plane moves its position in the sky. This is normally monitored by radar equipment and it
results in the x, y and z coordinates of the plane being updated.
• Data about a plane’s position is sent to a controller on the ground. The object world remains the
same; the only thing that happens is that the x, y and z co-ordinates of a plane are sent to the
outside world.
The way in which the object world within the shaded area is altered and accessed is achieved via a
mechanism known as a message. An example best explains what a message looks like. The line below shows a Java message being set to the aeroplane AF565; the effect of the message is to update the position of the plane to which the message is sent. At this stage don’t worry about how such messages are programmed in Java; the important thing you should be concentrating on at this stage in the book is the format of messages:
AF565.newPos(12, 444, 22);
The line consist of two components. The first component is an object known as the receiver object.
In the line above, this is the plane identified by AF565. The second component is the message. The
message above consists of four components. The first component is known as the selector, which
identifies the message that is to be sent. The remaining three components are known as the arguments
of the message. Thus, in the example above, the receiver object AF565 receives a message identified
by the selector newPos and arguments 12, 444, 22 which are associated with the x position, y position and z position of the plane. Do not worry about the semicolon that is at the end of each statement. As we have implied above, messages are generated by events which occur in the outside world.
The message example above might correspond to the event of a plane moving and its new position
being detected by a radar. Messages either update objects, update objects and return with some value
or just return with some value without updating any objects. Another example of a message associated with the air traffic control system is:
LH123.getxPos();
This message is sent to the receiver object LH123 and returns with the x position of the plane. Here
the selector is not associated with any parameters so the brackets following it are empty. This message is differentiated from the previous one that we examined in one way: it does not have
any arguments. A message in Java can consist of an arbitrary number of arguments ranging from zero
up to some arbitrary number imposed by the Java interpreter you use.
Another example of a message is:
AF555.getyPos();
This sends the message getyPos to the receiver object AF555 and returns with the y position of the
plane. Another example of a message associated with the simple air traffic control system is:
BA126.removePlane();
This removes a plane from the air space. This message would normally be sent when a plane flies outside the air space covered by the system. A final example of a message is:
BA133.landPlane();
This would result in the receiver object (BA133) landing and disappearing from the world of plane
objects. It is worth pointing out that the messages which we have discussed are all associated with
some Java programming code which carries out the processing associated with the message. However, at this point you should not worry too much about how this code is defined.
A more complicated air traffic control system
The previous example was used to describe some of the core concepts of object-oriented technology.
This example provides some reinforcement of these concepts and also introduces some new properties of messages.
The example involves an air traffic control system in which the planes are held in a number of queues. Each queue will have a collection of planes awaiting landing. Normally a plane lands by being transferred from the front of one queue to the end of another queue below it. The system also keeps track of the planes that have landed
This shows that the air space has been divided into three queues with the topmost queue containing
planes which have the longest time to wait before landing. The instance also shows a collection of
planes which have landed. The important point to make about this application is that not only does it
contain simple objects such as planes, but it also contains objects which themselves contain objects.
For example, the collection of landed planes is a set of plane objects and the queues of planes awaiting landing are also objects which in turn contain other objects (planes).
Since this is a more complicated system than that described in the previous subsection it will have
more messages associated with it. An example of a message is shown below:
queues.moveDown(1);
This takes the first plane in queue 1 and places it at the end of the queue below it (queue 2). The receiver object in this case is queues. This illustrates another important point about objects. The receiver object for this message is not a simple object such as a plane but a collection of objects.
Some more examples of the typical messages associated with this application are shown below:
queues.moveUp(2);
queues.land();
queues.movePlane(AF555,2,3);
queues.emergencyLand(BA7630);
The first message consists of a selector and one argument. Its function is to take the first plane in the
second queue and place it at the end of the queue above it; this message might be generated when an
air traffic controller is attempting to find some space in a queue – perhaps for a plane that wants to
land in a hurry.
The second message lands the plane which is nearest the ground. This message is a frequent one
since it corresponds to the normal operation of the air traffic control system and might be generated
when a controller lands a plane normally.
The third message, which consists of a selector and three arguments, moves a plane from its current
position to a new position within a queue. The example shown above moves the plane AF555 to
the third position in queue 2.
The final message, which just consists of a selector and a single argument, lands a plane in an
emergency: no matter where the plane is in the queues, it joins the planes which have already landed
on the ground. Clearly this corresponds to a major incident at the airport. Some further examples of
messages are shown below:
queues.moveUp(3);
queues.movePlane(TU878,1,3);
queues.adjustPlane(TU878,3,1);
queues.land();
With the first message the plane which is at the head of queue 3 is moved up so that it is at the end of
queue 2. Next, the plane TU878 is sent to the third position in queue 1. Then, the same plane is sent
to the first position in queue 3. The next line of code results in the plane at the head of the lowermost
queue (queue 3) being landed. This is plane TU878.
Again it is important to point out that each of the messages shown above is associated with program
code which is executed when the message is sent to the receiver object.
A print spooler
The final example that I will discuss is a print spooler. This is a piece of system software that forms
part of a multi-user operating system. During the operation of such an operating system computer users will ask for files to be printed. The computer normally has limited printing resources and so the
print requests are queued up for each printer in the system, with the print spooler controlling the addition of print requests to the queues and the selection of those print requests that are to be satisfied.
This shows a number of objects. The first is a table which lists all the printers in the system together
with their upper and lower print limits. For example, one entry in the table states that printer TH44
will only be available to print files between 5000 to 10 000 lines of text; obviously the technology
used in this printer is best for
large print runs. Each item in the table is itself an object, so in Figure 2.3 the printer table currently
holds six objects which describe the printing limits of individual printers. One thing which is worth
noticing in Figure 2.3 is that we give objects names which start with lower case letters.
The other objects shown in this figure are the print requests (the shadowed boxes) which are identified by an initial letter p, and a collection which contains the names of the available printers together with a pointer to their queues of print requests – some of which could be empty. Each item in the collection is itself an object, the queues are objects and the print requests in the queues are objects. So it is quite a rich model.
A whole variety of messages might be generated during the execution of such a print spooler. For
example:
printQueues.addRequest(LA245,p107);
would add a print request identified as p107 to the end of the queue for the printer LA245. The message
addRequest(LA245, p107) would be sent to the object printQueues containing the queue information
for the spooler.
Another example of Java code containing a message for the spooler would be:
printQueues.removeRequest();
This shows the receiver object printQueues being sent the message removeRequest() which results in
the first item in the queue associated with this object being removed from the queue.
As well as the queues being associated with messages, the table holding print details would also
be associated with messages. For example, the Java code:
printTable.adjustPrinterLimits(LA24,100,3000);
contains a message sent to the receiver object printTable which adjusts the lower and upper limits of
print requests which the printer LA24 can handle, with the lower limit being 100 lines and the upper
limit being 3000 lines. The code:
printTable.newPrinter(LA777,100,7000);
would add a new printer to the table with a lower print limit of 100 lines and an upper limit of 7000
lines of code. This message would also give rise to a new message:
printQueues.setNewPrinter(LA777);
generated by the code associated with the newPrinter message which would establish a new object
corresponding to LA777 within the print request queues. This message would be generated by the
print table object rather than emanating from outside the system. This is an important point: up till
now I have described the fact that messages are generated from the outside world by events that happen
in that world. While this is true of many messages you will also find, throughout this book, that
messages can be generated by objects and sent to other objects.
A line of code which contains a receiver object and a message will be referred to in the remainder
of this book as a Java expression.
Tidak ada komentar:
Posting Komentar