Section 5.1. Objects

   

5.1 Objects

ColdFusion 5 introduced user -defined functions, which allowed us to write functions much as we would in JavaScript. In the Java programming language, functions are often referred to as methods . One way of making a distinction between them is that a function can be declared (as in ColdFusion) in a vacuum of sorts ”it needn't always refer to any other thing or be part of any other thing (such as an object or a class). ColdFusion allows us to write user-defined functions that stand on their own, and the language defines many such functions.

Specific objects (or classes) define specific methods, and you can only use them when you have access to them. When you call a static method like Integer.parseInt() , you are not using an instance of the Integer class. Static methods are methods of classes, not objects. No integer object is necessary to call its static methods, and even if you have an instance, a static method is always called on the class, not the object.

Probably the easiest way to start thinking about object orientation is to think of things in the world, since programs are generally written to represent real things in the world. While you probably do this already in ColdFusion, this is also where the major difference between ColdFusion and Java comes into play. For example, if you were designing a ColdFusion application for a car manufacturer, you would likely create a database design, modelling your database in such a way that you would be able to represent everything your program needed to know about cars . Different tables would hold data about different aspects of the model. The car table might need to hold information about the make, model, year, color , and so on. A database design defines a representation of something in the world that will be interrogated by your program. A good design can answer all of the questions that will be asked of it in a timely fashion.

While databases are frequently used in Java programs, there is another layer of abstraction to help you model your data at the programmatic level: the object.

One way that might help you start thinking in terms of objects is to think about how you are used to representing data in a database. Instead of defining only the fields (that is, the data that your object contains), you also define what the object is capable of doing (the methods). This is where things start to depart dramatically from working with ColdFusion. Generally, the database defines the data and the CF code defines what you do with it. In Java, the data and what can be done with the data are both stored in objects. Classes are templates for making objects.

Note

There are, of course, object-relational databases and object-oriented databases that store data in a hierarchical manner. One such popular database is PostgreSQL, which is an object-relational database ”sometimes called a relational database capable of object-oriented extensions. That means that the database holds data, but that the concept of the table is extended as a class. Classes contain attributes (the columns ), which are defined as a set (the table). The difference is that classes can inherit from other classes, which makes the model not only relational but also logically hierarchical. You can also define functions inside the database itself using either an internal language or an external language (such as C).

PostgreSQL is gaining more interest among ColdFusion users. It began as a project of the University of California at Berkeley in the late 1970s. There, a team created one of the early relational databases in a project called Ingres. The Ingres code base was peddled by a company named Ingres that tried to break into the database market. In the late 1980s, a team led by Michael Stonebraker developed the code into an object-relational database, which was also turned into a commercial product called Illustra. Informix later purchased the Illustra company and merged the code into its database product. As the academic project continued , the code base for PostgreSQL remained distinct. It is currently open -source and free. You can download PostgreSQL from www.postgresql.org.


A car, a person, and a ball are all objects. These objects all have certain things that you can do only with them. For instance, a car object might define methods for start, accelerate, and brake. When trying to determine how to write your program in Java, you need to determine what objects you need to represent the problem domain that your program addresses.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net