This lesson is where you really start to understand the power of using an event-based, object-oriented programming model. In the tasks that you perform during this lesson, you will see that architecting your application properly will result in a much more scalable, reusable, and maintainable application. You will use proven methodologies to develop client-side data models in both MXML and ActionScript, which is an essential part of using the MVC (model-view-controller) design pattern that will be expanded on later in the book. You will receive an introduction to object-oriented design patternsspecifically, the value object pattern that will make data easier to manage by encapsulating data in one object. This pattern can also help to increase network performance by encapsulating various data structures into a single request, instead of making many separate calls to pass complex data structures. You will use the value object pattern in the application to manage all the client data structures you are building.
In this lesson, you will also use the built-in events of ActionScript 3.0, a powerful event-based programming language available to you in Flash Player. An event can be a simple user action, such as clicking, a system action, such as loading the last byte of data from a server, or a more powerful custom action that can be broadcast throughout an application. To respond to any event you must write an event handler, which is a function (or method) in an <mx :Script> block. This event handler will be called only when the event is broadcast (when a user clicks the button).
To fully understand the event-based programming model of ActionScript 3.0, it is important to understand that each MXML tag that you add to an application represents an object. Objects consist of characteristics that describe them, called properties, and behaviors that describe what they can do or what can be done to them, called methods. Properties are just variables attached to an object and methods are functions attached to an object. For example, a Flex Label object has a property describing the text appearing in the label (the text property). It also has behaviors; for example, the setStyle() method that enables you to change the font of the Label controls.
The concept behind object-oriented programming languages is that computer systems modeled after the real-world environment that they represent are more adaptable. What does this really mean? Hopefully, the concepts of "computer systems," "real-world environment," and "adaptability" are easily understood, but what about the concept of "modeling?" Modeling refers to how a computer system is designed. Thus, a computer system modeled on real-world entities refers to an application that is composed of units with characteristics and behaviors similar to real-world complements.
For example, in a grocery store, an employee is responsible for dealing with product descriptions, categories, list prices, costs, and so on for each product the store sells. Therefore, an object-oriented system to support the store will have an entity representing each grocery item; this entity is called an object. The closer the characteristics and behaviors of this object in the computer system resemble those of a true grocery item, the easier it is to adapt the system to changes in the business.
Objects are created based on a class definition, which is much like an architectural blueprint. A blueprint identifies what a house will look like and what amenities it will have. A blueprint can be used many times to create many houses and it guarantees similarities in all houses built from the blueprint. A blueprint might include details such as the following:
Collectively, the elements describe the final shape the house takes and how it performs. In object-oriented terms, these are the properties of a house, and would be listed in a class definition.
What if a single architect had to design every element of a new house? Finding an architect who is also a plumbing and heating expert might be a challenge. A better choice would be to have a heating expert design and build the heating system, a plumber to do the plumbing work, and so on. In fact, many of the components of a house are prebuilt. For example, you generally purchase a heating and cooling system, and rarely design your own. Using these prebuilt pieces, the complexity of building a house is greatly reduced.
Object-oriented programming embraces the same methodology, encouraging the use, and reuse, of prebuilt objects, which are tested and guaranteed by experts in the domain. Individual objects can be used interchangeably without redesigning the entire system. As with a home, the complexity of designing and building a new application is greatly reduced by using prebuilt pieces. Continuing with our house metaphor:
In this lesson, you will create custom ActionScript objects to mirror real-world business problems associated with an online grocery store application. You will build custom ActionScript classes that will define each product in inventory and handle customer purchases. You will understand the process for creating objects which are aware of their properties (id, name, description, price, and so on), and you will create methods for interacting with the data. You will learn about and encapsulate data using the value object pattern, which will result in a more maintainable application.