In our study of objects, we will find that an object is a software representation of a real-world item, or object. Software objects feature properties (that describe the object), methods (that allow the object to do things for you), and events (code that is executed automatically when a certain situation - an event - occurs).
Once we have looked at what an object is, we'll then be able to look at how objects are useful to us in developing ASP applications. Developing web applications requires us to deal with both the client-side and server-side programming, and therefore we'll take a look at how objects can be used on both sides of the wire.
Before we get started, here is what we will cover in this chapter:
- What is an object?
- An introduction to properties, methods, and events
- How can we change the characteristics of an object?
- How do we know when the object tells us something?
- What is an object model?
- How do we use the object model in Active Server Pages?
We'll begin by taking a look at what an object is.
What is an Object?
In the real world, we already know what objects are. They're the things we look at, pick up, and use every day - things like our chairs, our telephones, and our computers. All these are solid, tangible entities.
However, if we want to describe a telephone to somebody in abstract terms, we can do this by talking about it in terms of its essential characteristics - what properties it has, what it can do, and how we can interact with it. All telephones have these characteristics, and we can use them to establish exactly how one telephone differs from the next.
So, for our telephone's physical properties, we could say that it has a microphone, a speaker, and a dialing pad. We could also say that it lets us do things, such as call someone and talk to them. Our telephone will also tell us when certain events are happening: for example, if a friend is trying to call you, your telephone will ring to let you know. This ringing will prompt you to take some action, like picking up the handset and talking to your friend. As an abstract object, our telephone has:
- Certain properties that define and describe it
- A set of things or methods that it lets us do
- The ability to prompt action when events occur
We can use these three attributes to describe physical objects and abstract concepts. In a few minutes we will describe how these real-world ideas are replicated in software objects, but for now lets go a little deeper into our real-world telephone. By learning about what objects are, we can then look at how to use them in a way known as object-based programming. In the object-based way of programming, the application is broken down into a set of objects. In doing this, you can build the application in a two stage process. Firstly, you create the objects you will need in your application and then you set up the relationships and interactions between objects. Later in this chapter, we will see how the objects of Active Server Pages relate and interact with each other and allow us to build our applications.
Our Telephone in Detail
Here is our telephone. To look at this as an object, let's put down some information about it. We will be classifying the information into three categories:
- Things that describe the telephone
- Things that we can do with the phone
- Things that the telephone tells us and that we can react to
Let's look at each of these aspects in turn:
How It Works
The three categories that we have created in the left-hand column can be applied to any object. In fact, the best way to describe an object is to break down its characteristics into these three categories, and put information about your object into these categories. Any information that you have about a particular object can be included in one of these categories.
If you have another telephone that features all these characteristics, except that its color is blue, then we can describe your telephone by changing that one part of the description above. Moreover, this technique works for any type of object, both real world and software.
So far, we have used verbose English terms to describe our three categories. In the world of objects, we need terms that concisely describe each of these three categories. These terms are properties, methods and events. In addition to these terms, we need to look at the term instance as it relates to objects. In this section, we'll look more carefully at what each of these means in abstract terms.
Instances and Classes
When we are talking about a unique object, we can use the term instance to say that we are talking about a particular telephone object - your telephone for example - that has a specific set of properties and values. When we want to talk about another telephone, we use a different instance of the telephone object. In this way, both you and I can have instances of the telephone object. For example, my telephone (my instance of a telephone object) is gray and comes with special answer-phone facilities, your telephone (your instance of a telephone object) may be red, blue, green etc. These instances represent completely different physical objects. However, since they are both instances of the same object description or template, they share the same types of characteristics such as methods, properties (although the values can be different), and events. When a specific instance of an object is created from the template for the object, the object is said to have been instantiated. What actually happens is that a copy is made of all of the properties and events from the object description, but the methods (frequently a big chunk of code) remain in the original place and this section of code is used by all of the different instantiations of that one object.
So we've mentioned object descriptions or templates, but it's time to give them their proper name in ASP; classes. We mentioned that each object can have different instances. For instance, my telephone is small and white and has 12 buttons. Your telephone will probably be different to that, but they're both recognizable as telephones and they both provide the same function. They both conform to a set of rules for what a telephone does - they connect to the local telephone line, they both have a numeric keypad and somewhere to speak into. A class in ASP is like a set of design rules that an object must conform to. It would be no good if my telephone didn't have a handset, or a numeric keypad, even if it did plug into the telephone socket on the wall. In a class there should be a minimum set of functions that your object must be able to perform.
When talking about those characteristics that describe an object, we are talking about the properties of the object. Each property of the object describes a particular aspect of the object. The property is actually described as a name/value pair. This means that for every named property, there is a single unique value that describes that property for this instance of the object. If we go back to our telephone example, we can create a table that lists each of the property names and the value of each property.
We now have a set of properties that describe this instance. The properties of an object are used to represent a set of values associated with the object. A new instance of the object may have different property values, but it has the same property names.
Even with different property values, these two telephones are instances of the same object template. Since we know that all telephone objects have a 'Color' property, we can determine the color of each of the phones by examining its 'Color' property value. We can use properties in two ways. We can read from them or we can also write to them. So if we wanted, we could have changed the cover of our telephone to a different color, if we required.
Now that we have a way of describing the telephone object, let's take a look at what we can do with it.