| By Hal Helms | Article Rating: |
|
| March 11, 2006 02:00 PM EST | Reads: |
18,323 |
Let me offer a very simple example. (The problem with most such simple examples is that without any real-world complexity, we can think of so many other, more straightforward ways than the one presented.) Our example begins with a Truck.cfc.
<cfcomponent displayname="Truck" extends="BaseComponent">
<cfset variables.instance.odometer = 0 />
<cffunction name="init" access="public" output="false">
<cfargument name="odometer" required="true" />
<cfset set('odometer', arguments.odometer) />
<cfreturn this />
</cffunction>
</cfcomponent>
Let's say further that we have several of these objects in memory, but I want to make one of them a guinea pig for a new GPS class I've written. The code for GPS.cfc is as follows:
<cfcomponent displayname="GPS" extends="BaseComponent">
<cfset variables.instance.locations = null />
<cffunction name="init" access="public" output="false">
<cfset var locations = ArrayNew(1) />
<cfset locations[1] = "Tony's Pizzeria" />
<cfset locations[2] = "Town library" />
<cfset locations[3] = "On route to next stop" />
<cfset locations[4] = "Office" />
<cfset locations[5] = "Starbucks" />
<cfset locations[6] = "Casino" />
<cfset locations[7] = "Gas station" />
<cfset locations[8] = "Quarry" />
<cfset variables.instance.locations = locations />
<cfreturn this />
</cffunction>
<cffunction name="locate" access="public" output="false">
<cfset var locations = get('locations') />
<cfreturn locations[RandRange(1, 8)] />
</cffunction>
</cfcomponent>
I don't want to change the definition of Truck - at least not yet. Instead, I'd prefer to take a single Truck object and mix the methods and instance variables of GPS into it. Object-based mixins make this very easy, indeed. Here is code to create a Truck, a GPS, mix the GPS into the Truck and call a method on that Truck that previously belonged only to GPS, locate:
<cfset truck = CreateObject('component', 'Truck').init('Big Red') />
<cfset gps = CreateObject('component', 'GPS').init() />
<cfset truck.mixin(gps) />
<cfoutput>
#truck.locate()#
</cfoutput>
The Universal Superclass
Skeptical readers may be forgiven for examining the Truck.cfc only to find that it has no mixin method. For that matter, several of the instance variables I used in these examples have been initialized to null. But ColdFusion has no null. What's going on?
Several OO languages have the notion that classes that do not explicitly extend another class implicitly extend a single universal superclass. Java has the Object class and ColdFusion has component.cfc, found in the web root's WEB-INF/cftags directory. Set a variable (such as null) in this component and it will be available to all components. Write a method (such as mixin) in this component and it will be available to all components too.
While adding things directly to component.cfc is perfectly fine, I've chosen a slightly different course: I've created a BaseComponent.cfc. All components that don't already extend a component in the domain model extend BaseComponent, ensuring that all CFCs inherit from BaseComponent.
A BaseComponent class is the perfect spot to locate our mixin method, which is a model of simplicity:
<cffunction name="mixin" access="public" output="false">
<cfargument name="mixinCode" required="true" />
<cfset StructAppend(this, arguments.mixinCode) />
<cfset StructAppend(variables, arguments.mixinCode) />
<cfset arguments.mixinCode.inject(this) />
</cffunction>
In our example of the Truck and GPS, we call the truck's mixin method (which it inherits from BaseComponent), passing it the mixinCode - the GPS object). As part of mixin, the mixinCode's inject method is called. The mixin method adds the methods of the GPS to the Truck, while the inject method adds the private instance variables of the GPS to the Truck.
<cffunction name="inject" access="public" output="false">
<cfargument name="mixee" required="true" />
<cfloop collection="#variables.instance#" item="aKey">
<cfset arguments.mixee.set(aKey, variables.instance[aKey]) />
</cfloop>
</cffunction>
In Closing
For too long, we've been treating ColdFusion components as if they were a weakened version of Java classes. This (usually unstated) assumption has hidden from us the real power of CFCs. Mixins illustrate how we can find new ways of making software that's more flexible and maintainable. Unlocking the power of CFCs is possible only when we embrace CFCs as they are - and not as poor substitutes for Java.
Published March 11, 2006 Reads 18,323
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Hal Helms
Hal Helms is a well-known speaker/writer/strategist on software development issues. He holds training sessions on Java, ColdFusion, and software development processes. He authors a popular monthly newsletter series. For more information, contact him at hal (at) halhelms.com or see his website, www.halhelms.com.
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Betas Target RIAs and Cloud Computing
- Adobe Cans Another 9% of its Workforce
- Moyea DVD4Web Converter V2.0 Converts DVD to FLV Fast and Synchronously with Watermarks
- Adobe Fiddles with its Web Apps
- Adobe & Salesforce Cut Cloud Deal
- Hosting.com Launches ColdFusion 9 in the Cloud
- The Real Time Infrastructure Ultimatum
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Is Microsoft as Free as Open Source?
- Adobe Reader Sued
- The Planet Named “Bronze Sponsor” of Cloud Computing Expo
- Microsoft Expression Web Has Got Game
- Adobe Flex Developer Earns $100K in New York City
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Bruce Chizen Joins Voyager Capital as Venture Partner
- My Top Seven Wishes From Adobe MAX 2009
- The Next Programming Models, RIAs and Composite Applications
- Where Are RIA Technologies Headed in 2008?
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- Has the Technology Bounceback Begun?
- Building a Zip Code Proximity Search with ColdFusion
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- The Asynchronous CFML Gateway
- Web Services Using ColdFusion and Apache CXF





























