Events and messages are two concepts vital to actually using objects in a computer program. An event as defined in computer science is not too different from your own personal definition of the term “event.” An event in the context of computer science, and in daily life, means “something happened to catch my attention.” An example of an event in daily life would be a lightning strike or the ring of a doorbell. Each of these events would catch your attention and perhaps cause some response from you. In computer science, the click of a mouse or tap of a key is an event. Likewise, the computer will take notice and might respond to the event if it has been instructed to do so by the program that is currently running. Just as you would not likely respond to the neighbor’s ringing doorbell, the computer doesn’t necessarily respond to every mouse click or key tap that occurs. A message is an instruction sent to an object at the occurrence of an event that requests the object to perform one of its methods.
Event - An action such as a mouse click or a key press which is recognized by a program and may prompt some action by the program.
To better understand this concept of events and why it’s important to OOD, let’s look over the shoulder of a DMV clerk as he or she interacts with a customer. The interaction might go like this:
Clerk: “Hello. What can I do for you today?”
Customer: “I would like to renew my vehicle registration.”
Clerk: “What is your license number, please?”
Customer: “385-CJK”
Clerk: Enters the numbers 385-CJK into a field on the computer screen and clicks on a “Find Registration” button.
Behind the scenes the button click action by the clerk is interpreted by the computer as an event – Ta-Da! Something happened! Because of the code in the vehicle registration program, the computer responds by sending a message to the computer system to find in its memory the specific and unique vehicle object associated with this number. Next it sends a message to that specific vehicle to display some of its properties on the screen where the teller sees Kim’s name, address, and description of the vehicle and the calculated the registration fee.
Clerk: “This year’s fee is $180.00. Has any information changed in regard to this vehicle, Kim?”
Customer: “Yes, my address has changed. I now live at 23 S. New Haven Street.”
Clerk: Enters “23 S. New Haven Street” into a field and clicks on the “Update Owner Information” button.
Another button click event has occurred and a message is sent to the vehicle object to change the recorded address of the owner. And perhaps yet another message is sent to this vehicle object to send its updated owner information, as well as the renewed registration to the printer.
Clerk: “Your registration has been renewed and your address changed. Here is your new registration copy for the glove box.”
Customer: “Thank you”
Clerk: “You’re welcome, Kim. Please come again”
Clicks on the “End Transaction” button initiating another event which sends a message to the system to close access to this particular vehicle.
How does this occur inside the code? After a programmer writes the code for the Vehicle class (“vehicle” object factory) to create these vehicle registration objects, he or she creates another segment of code called the graphical user interface (GUI) which interacts with the class, and objects created from the class, through messages. The graphical user interface is the part of the program visible to the user – in this case, the DMV clerk. When a vehicle owner registers a new vehicle, the clerk uses the GUI part of the program as a tool to call upon the class by sending a message to create a specific vehicle object with a unique set of vehicle properties for this specific customer. In computer terms, this step to create new, unique objects and store them in memory is called instantiation. The Vehicle class creates an “instance” of the class. This instance is the object. In other words, the Vehicle class creates a new vehicle object based upon the specific “blueprint” for a vehicle as defined in this “vehicle factory” class and stores it in the computer memory.
Instantiation - The creation of a new object from specifications described in a class.
This is what the code to create a new object from the Vehicle class looks like in pseudo-code (human-readable words):
Create MyCar as a new Vehicle(list of property values)
Conceptually, the clerk uses the graphical user interface to trigger events which send messages to the class. These messages can be commands either to classes to create new objects, or to previously created objects to execute methods. In the DMV example, the clerk uses the application by entering information and clicking on buttons which create events that cause messages to be sent to the Vehicle class to create new vehicle registrations or to record transactions for a specific registration.
Occasionally a message sent to an object must carry with it some additional details. A “Update Information” message sent to a specific Vehicle object must carry with it new information about the persons name or address. This additional information sent with a message is called a parameter.
Parameter - A value sent to a method with a message that is needed to perform the work of the method.
This is what the code to send additional information in the form of a parameter looks like in pseudo-code when the message to update information is sent to a specific vehicle:
MyCar.UpdateInformation(23 S. New Haven Street)
Get Real Let’s explore events, messages, and parameters in a common computer application. |
Open Microsoft Word or other word processor. Write a short paragraph describing your understanding of OOD at this time. Now experiment with some of the common commands you use as you edit in a word processor. Complete the chart below.
|
Task | Event | Message | Parameter |
Find | | | |
Insert hyperlink | | | |
Underlining text | | | |
Because there are several ways to perform the indicated tasks your answers will likely vary from the answers below. The important concept is to analyze what event you are triggering, the message being sent to the computer by that event, and the additional data or parameters the computer will need to complete the intended task. |
Task | Event | Message | Parameter |
Find | Mouse click Enter Key pressed | Open find dialog box Search through text | None Characters to find |
Insert hyperlink | Mouse click Return | Open link dialog box Add link to text | None URL |
Underlining text | Mouse click & drag Mouse click on U | Select chosen text Underline text | None Selected characters |