< Day Day Up > |
Effective communication is a fundamental requirement for success. In the workplace, the required communications often take place between people: co-workers strategizing together, account managers working with clients , and so on. In these cases, a lack of communication can put a tremendous strain on the system (whether that system is an efficient office, vendor-client relations, or even software systems). In the object-oriented world, the entities that need to effectively communicate are the objects themselves . Communications between objects in the object-oriented world can happen in one of two ways: with messages or with events. Messages as Inter-Object CommunicationsMessages are a fancy way to refer to an object calling the method of another object. In Chapter 4, "Method to the Madness," we examined methods in depth. Methods of objects play an important role in inter-object communication because they are used as the receiver of messages within ActionScript 2.0. What this means is that for an object to send a message to another object, the sending object will invoke a method on the calling object. To invoke a method, we simply write the object name, a dot, the method name , and a pair of parentheses that can contain any data to be passed as part of the message (referred to as the arguments of the message). A message in ActionScript 2.0 is nearly identical to the equivalent message in ActionScript 1.0, with the exception that strong datatyping is now available. A simple message might look like this: var thePayment:Number = myCarLoan.getMonthlyPayment(); In this example, the object making the call (the sender) is calling an object named myCarLoan (the receiver) and is passing no specific data to it (hence the empty parentheses). The receiver of a message can respond through its return statement, and the results will be stored in the new variable thePayment . In Chapter 5, "The Meek Shall Inherit the Earth," the getMonthlyPayment() method was defined like so: public function getMonthlyPayment( ):Number { var monthlyPayment:Number = super.getMonthlyPayment() + this.getBankFee(); return monthlyPayment; } This method signature tells us that when called, it will always return a numeric value. In this case, the number is computed in the local variable monthlyPayment . By specifying a return type for methods, the calling objects can know what to expect when the receiver responds. In the first example, a message is sent to the getMonthlyPayment() method of the myCarLoan object. The message is asking myCarLoan to respond with the monthly payment for that loan. When getMonthlyPayment() responds to the message with the requested data, it is assigned to a variable named thePayment , which is defined to hold only a number. Having the ability for objects to call methods on other objects might not seem important initially; however, it is this simple concept that enables our systems to be as flexible and maintainable as they are. With the ability to define any functionality once, and only once, as a method of an object, and with the ability to interact with that method from any other object in the system, there is no need for redundant code. Each block of functionality exists in only one place, so if it needs to be enhanced or maintained , there is only a single block of code that will change. This greatly reduces the complexity (and therefore the cost) of maintaining and extending applications. Events as Inter-Object CommunicationsThe other type of communications that can occur between objects is the event. An event is any action that can be captured in code. Often times, these actions are driven by users who might, for example, be clicking a button. Other times, these actions, such as a screen being revealed, are driven by the system.
The prevalence of OOP has greatly increased in the past decade or so. This increase is directly related to the growth of the popularity of graphical user interfaces (GUIs) in modern applications. One key reason for this relationship is that object-oriented languages have a native ability to handle events. A GUI by its nature is a hot bed of events. Each time the user interacts with items on the screen (such as by clicking, dragging, typing, and so on), an event is fired . It is the event-handling nature of object-oriented languages that makes the GUIs possible. There are two types of events that are encountered in OOP: events that occur as a result of a user's action, known as user events, and events based on actions within the application itself, known as system events. User EventsThe events most familiar to Flash developers and designers are driven by the end user. Each time a Flash application responds to the user's interaction, it is done through events. Each class in Flash has events to which it can respond. Table 7.1 shows some user events and the classes to which they belong. Table 7.1. User Events and the Classes to Which They Belong
Although this list is not comprehensive, it clearly shows that there are quite a few end-user actions that can drive events within applications.
System EventsAlthough many events are driven by the end user, some events are purely system events. These events react to specific application functionality, such as an external file being loaded or an error being thrown. We will see an example of a system event being handled a bit later in the chapter, when we discuss callbacks. (In that section, Listing 7.2 shows the process of loading a file into Flash and responding to the system event, which fires when the file has completely loaded.) Table 7.2 shows system events for a few classes in Macromedia Flash MX 2004 Professional. Table 7.2. System Events and the Classes to Which They Belong
Again, although the list of classes with system events shown in Table 7.2 is not comprehensive, it is still important. For instance, consider the onResult event of the WebServices class. Often, data from a web service will be used to populate the interface of an application. There is no use in attempting to work with the data until it has completely arrived in the Flash Player. The onResult event is fired to indicate that the data has finished loading.
|
< Day Day Up > |