As you have learned in this chapter, you can use custom tags to package whatever type of processing you wish. In the last chapter, you learned how to package custom processing with the simpler user-defined functions framework. An important part of ColdFusion is its ColdFusion Components framework. Think of the CFC framework as a special way to combine key concepts from custom tags and user-defined functions into objects. These objects might represent concepts (such as individual films or actors), or they might represent processes (such as searching, creating special files, or validating credit card numbers). About ColdFusion ComponentsYou can think of CFCs as a structured, formalized variation on custom tags. Whereas custom tags are very free in form and don't necessarily imply any kind of "correct" way to go about your work as a developer, the CFC framework gently forces developers to work in a more systematic way. If you choose to use the CFC framework for parts of your application, you will find yourself thinking about those aspects in a slightly more theoretical, abstract manner. And this has lots of benefits. Because CFCs are more structured, the code is generally very easy to follow and troubleshoot. Think of the CFC framework as a way to write smart code, guiding you as a developer to adopt sensible practices. But the most dramatic benefit is that the structured nature of CFCs makes it possible for ColdFusion to look into your CFC code and find the important elements, such as what functions you have included in the CFC and what each function's arguments are. This knowledge allows ColdFusion to act as a kind of interpreter between your CFC and other types of applications, such as Dreamweaver, Flash, and Web Services. If you want them to, these components become part of a larger world of interconnected clients and servers, rather than only being a part of your ColdFusion code. CFCs Can be Called in Many Different WaysThis chapter and the last have been all about making it easier to reuse the code that you and other developers write. CFCs take the notion of code reuse to a whole new level, by making it ridiculously easy to reuse your code not only within ColdFusion, but in other types of applications as well. All of the following can share and use CFCs:
In other words, if you like the idea of reusing code, you'll love the CFC framework even more than the UDF and custom tag frameworks. CFCs are Object-Oriented ToolsDepending on your background, you may be familiar with object-oriented programming (OOP). Whether you know OOP or not, CFCs give you the most important real-world benefits of object-oriented programming without getting too complicatedexactly what you would expect from ColdFusion. NOTE Don't let the OOP term scare youthis isn't your father's object orientation. The introduction of CFCs hasn't turned ColdFusion or CFML into a complex, full-blown object-oriented language. CFCs aren't obsessive-compulsive. Whether your father may be is a different story. Without getting too deeply into the specifics, you can think of object-oriented programming as a general programming philosophy. The philosophy basically says that most of the concepts in an application represent objects in the real world, and should be treated as such. Some objects, like films or merchandise for sale, might be physical. Others, like expense records or individual merchandise orders, might be more conceptual but still easy to imagine as objectsor objectified, like many of Orange Whip Studios' better-looking actors. ColdFusion's CFC framework is based on these object-oriented ideas:
CFCs and Method InheritanceIn addition to the OOP concepts just listed, ColdFusion's CFC framework also supports the notion of inheritance, where different object classes can be derived from a parent object class. There might be other types of objects that are related to the Film class, but have additional, more specific characteristics (perhaps ComedyFilm, which would track additional information about how funny it is, and ActionFilm, which would track how many explosions occur per minute). NOTE Once you start talking about this concept, you usually need to start using other object-oriented vocabulary words, such as subclassing, overriding, overloading, descendants, polymorphism, and so on. If none of those words means anything to you, don't worry. The CFC framework implements the concept in a straightforward way that sidesteps many of the thornier points of traditional OOP implementations. This isn't your father's inheritance.
You can learn all about CFC inheritance in this books companion volume, Advanced Macromedia ColdFusion MX 7 Application Development. See also the extends attribute of the <cfcomponent> tag in Appendix B. The Two Types of ComponentsMost CFCs fall into two broad categories: static components and instance-based components. Static ComponentsI'll use the term static to refer to any component where it doesn't make sense to create individual instances of the component. Often you can think of such components as services that are constantly listening for and answering requests. For instance, if you were creating a film-searching component that made it easy to search the current list of films, you probably wouldn't need to create multiple copies of the film-searching component. Static components are kind of like Santa Claus, the Wizard of Oz, or your fatheronly one of each exists. You just go to that one and make your request. Instance-Based ComponentsOther components represent ideas where it is very important to create individual instances of a component. For instance, consider a CFC called ShoppingCart, which represents a user's shopping cart on your site. Many different shopping carts exist in the world at any given time (one for each user). Therefore, you need to create a fresh instance of the ShoppingCart CFC for each new Web visitor, or perhaps each new Web session. You would expect most of the CFC's methods to return different results for each instance, depending on the contents of each user's cart. |