YOUR FEEDBACK
Jeremy Geelan wrote: In response to inquiries and suggestions from readers this lexicon has recently...
AJAXWorld RIA Conference
$300 Savings Expire August 29
Register Today and SAVE!


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP COLDFUSION LINKS


Mixins
Writing software that's more flexible and maintainable

The only thing needed is to include Teacher.cfm and write an init method - a sort of constructor for our CFC.

Our StudentTeacher.cfc is similar:

<cfcomponent displayname="StudentTeacher" extends="Student">
    <cfinclude template="Teacher.cfm" />

    <cffunction name="init" access="public" output="false">
      <cfargument name="average" required="true" />
      <cfargument name="name" required="true" />
      <cfargument name="test" required="false" default=null />
      <cfset super.init(arguments.average, arguments.name) />
      <cfset set('test', arguments.test) />
      <cfreturn this />
    </cffunction>
</cfcomponent>

Without extending or implementing anything, we have the one authoritative, unambiguous representation of what a teacher is.

That's not to say, though, that we can't extend classes. In fact, StudentTeacher extends Student.cfc:

<cfcomponent displayname="Student" extends="BaseComponent">
    <cfinclude template="Student.cfm" />

    <cffunction name="init" access="public" output="false">
      <cfargument name="average" required="true" />
      <cfargument name="name" required="true" />
      <cfset set('average', arguments.average) />
      <cfset set('name', arguments.name) />
      <cfreturn this />
    </cffunction>
</cfcomponent>

Student is similar to Teacher and includes the one authoritative, unambiguous representation of what a student is: Student.cfm:

<cfset variables.instance.average = 0 />
<cfset variables.instance.name = null />

<cffunction name="study" access="public" output="false">
    <cfreturn "Yes, yes, I'm studying..." />
</cffunction>

<cffunction name="takeTest" access="public" output="false">
    <cfargument name="test" required="true" />
    <cfif arguments.test.get('percentOfGrade') LT 10 AND get('average') GTE 100>
      <cfreturn skipTest() />
    <cfelse>
      <cfreturn workHardOnTest(arguments.test) />
    </cfif>
</cffunction>

<cffunction name="skipTest" access="private" output="false">
    <cftrace type="Information" text="I think I'll just skip this test.
    I've already got a #get('average')#" />
</cffunction>

<cffunction name="workHardOnTest" access="private" output="false">
    <cfargument name="test" required="true" />
    <cftrace type="Information" text="I'm taking the test: #arguments.test.get('name')#" />
</cffunction>

We should make sure all this works:

<cfset anne = CreateObject('component', 'Student').init(94, 'Anne Baker') />
<cfset bob = CreateObject('component', 'Student').init(82, 'Bob Carter') />

<cfset carla = CreateObject('component', 'StudentTeacher').init(100, 'Carla Davis') />

<cfset test = CreateObject('component', 'Test').init('OO Development with ColdFusion', 8) />

<!--- have the Teacher do its thing--->
<cfset teacher = CreateObject('component', 'Teacher').init(test) />
<cfset teacher.addStudent(anne) />
<cfset teacher.addStudent(bob) />
<cfset teacher.addStudent(carla) />

<cfoutput>
     #teacher.assignHomework()#<br>
     #teacher.giveTest()#
</cfoutput>

<!--- now let's have the StudentTeacher --->
<cfset carla.set('test', test) />
<cfset carla.addStudent(anne) />
<cfset carla.addStudent(bob) />

<cfoutput>
     #carla.assignHomework()#<br>
     #carla.giveTest()#
</cfoutput>

Though it's not important for this discussion, I've included the code for Test.cfc for completeness. It can be found in Listing 1.

Object-Based Mixins
Class-based mixins work well when we want to build mixins into our design. As their name suggests, class-based mixins affect all instances of the class. Sometimes, though, that's not what we want. We may have objects in memory that we need to make structural changes to; we may need to affect some, but not all, members of a class; or we may be using code that we have no ownership of and no ability to alter.

In any of these cases (and more), a class-based mixin is not appropriate. Object-based mixins provide the ultimate in flexibility - even during runtime. Object-based mixins let us "inject" all methods and variables from one object (not class) into another object.

About Hal Helms
Hal Helms is a well-known speaker/writer/strategist on software development issues. His monthly column in CFDJ contains his Musings on Software Development and he has written and contributed to several books. Hal holds training sessions on Java, ColdFusion, and software development processes. He authors a popular monthly newsletter series. For more information, contact him at hal@halhelms.com or see his website, www.halhelms.com.

YOUR FEEDBACK
Najih wrote: In coldfusion server 5 the cfgraph daes not run with JRun. I have the message Could not connect to JRun Connector Proxy Please contact the system administrator for this web site.
Christian Cantrell wrote: Rob Burgess was not commenting specifically on the technology JetBlue uses for their web site. He was clearly making a higher level comparison between JetBlue's business pracitces and the opportunity industries have to deliver rich experiences using Macromedia products.
Patrick Scantland wrote: Ah ah Ron, that's a good one! JetBlue website is made with PERL for flight lookup and ASP.Net for everything else!.. What a commitment... we were feeling alone already!
CFDJ LATEST STORIES . . .
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted to be...
Red Hat CTO Brian Stevens, Citrix CTO Simon Crosby, Egenera CTO Pete Manca, Allen Stewart, Group Manager, Windows Virtualization at Microsoft, and Brian Duckering, Sr. Director of Products and Alliances at Symantec were the top industry executives who joined Jeremy Geelan in the 4th Fl...
Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft. Mike is focused on the delivery of the Windows virtualization technology, including Windows Server 2008 Hyper-V, Microsoft Hyper-V Server and Virtual PC 2007. Mike also directs the tec...
SQL Injection attacks are one of the easiest ways to hack into a website. One recent hack, using a script from verynx.cn, involves injecting sql into a web form that then appends some JavaScript code into fields in a database that then gets executed on the client side when a user views...
Recursion Software released a private beta version of their Voyager mobile platform, with powerful interoperability for Android, Microsoft .NET and Compact Framework (CF), all Java editions (JME CDC, JSE and JEE), and more than 15 embedded operating systems. The Voyager platform is a p...
2008 is going to be an important year for Rich Internet Applications. Most organizations are delivering or planning to deliver Rich Internet Applications; however, at the same time, most IT managers are facing a dilemma: which Rich Internet Application technology and platform to use? T...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE