Welcome!

ColdFusion Authors: Maureen O'Gara, Hovhannes Avoyan, Yakov Fain, Pat Romanski, Liz McMillan

Related Topics: ColdFusion

ColdFusion: Article

Mixins

Writing software that's more flexible and maintainable

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.

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.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.