YOUR FEEDBACK
ASP.NET
mark bosley wrote: Good article. Please post the code or send it to me. It ...
AJAXWorld RIA Conference
$300 Savings Expire July 25
Register Today and SAVE!


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
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

Digg This!

Page 1 of 3   next page »

In the last two installments of "Foundations," we looked at the issue of static v dynamic typing in ColdFusion and I argued that treating ColdFusion as a statically typed language led to disappointment and defeat. In this issue let's explore some of the possibilities available when we no longer treat ColdFusion as "Java Lite" but accept it on its own terms..

What Are "Mixins"?
In the late 1970s, a Lisp-based language called "Flavors", originating at M.I.T., introduced the idea of adding functionality from one class to another. The term, mixin, was apparently inspired by some of the ice cream parlors around the M.I.T. campus that offered customers the option of mixing such goodies as Oreos and candy bars into their standard ice cream flavors.

I've been working with Ruby lately, which also offers mixins, and I began to wonder if, freed from the constraints of static typing, (1) it might be possible and, (2) we might benefit from implementing mixins in ColdFusion.

After some thinking, talking with colleagues, and experimenting, I came up with two methods of introducing mixins and I've given them the names class-based mixins and object-based mixins. Both are quite useful but work very differently.

Class-Based Mixins
In the November issue of CFDJ, I explained how Java interfaces are used to provide for multiple-type inheritance while avoiding the problems of code conflict that multiple inheritance languages must deal with.

Java interfaces are wonderful - for Java - but they do violate a sound fundamental of creating software, dubbed the DRY (Don't Repeat Yourself) Principle. In an interview, Dave Thomas (of The Pragmatic Programmer fame) had this to say about DRY: "DRY says that every piece of system knowledge should have one authoritative, unambiguous representation."

Java interfaces push the "authoritative, unambiguous representation" of methods down to the implementing classes. There are many situations that arise where multiple implementing classes of a single interface will mean multiple code implementations.

Let me offer some example Java code to illustrate:

public interface Teacher{
     public void giveTest(Test test);
}

Here we have a very simple interface, defining what it means to be a teacher: a teacher is someone who can give a test. What does it mean to give a test? For our purposes, let's say that it entails the following:

  1. Give out Test booklets
  2. Answer any questions about the test
  3. Monitor the students
  4. Monitor the time
  5. Collect the test booklets at the end of the allotted time
Initially I want to have a Teacher class that implements the Teacher interface. It might look something like this:

public class Teacher implements interfaces.Teacher{
    private String name = null;

    public Teacher(String name){
      setName(name);
    }

    public void giveTest(Test test){
      giveOutTestBooklets(test);
      monitorStudents();
      monitorTime(test);
      collectTestBooklets();
    }
    // giveOutTestBooklets, monitor Students, monitorTime,
    // and collectTestBooklets methods below...
}

Now, I'll implement the Teacher interface in another class - say, StudentTeacher:

public class StudentTeacher extends Student implements Teacher{

    public StudentTeacher(){
      // default constructor
    }

    public void giveTest(Test test){
      giveOutTestBooklets(test);
      monitorStudents();
      monitorTime(test);
      collectTestBooklets();
    }
    // giveOutTestBooklets, monitor Students, monitorTime,
    // and collectTestBooklets methods below...
}

That doesn't look like one representation to me: we have a lot of duplicated code. Fortunately, class-based mixins can help us. Let's tackle the same problem but with ColdFusion with class-based mixins.

Our first step is to create a single representation of a teacher. We'll save this as a ColdFusion template (not a CFC) called Teacher:

<cfset variables.instance.name = null />

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

<cffunction name="giveTest" access="public" output="false">
     <cfargument name="test" required="true" />
     <cfset giveOutTestBooklets(arguments.test) />
     <cfset monitorStudents() />
     <cfset monitorTime(arguments.test) />
     <cfset collectTestBooklets() />
</cffunction>

<!--- other methods below... --->

This is the code we're going to mix into classes. Our first class is Teacher.cfc:

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

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


Page 1 of 3   next page »

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.

CFDJ LATEST STORIES . . .
Adobe's Kevin Lynch and Microsoft's Scott Guthrie to Keynote AJAX World RIA Conference & Expo
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
Voyager Offers Android, .NET CF, Java Runtime Support
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
AJAX and Enterprise RIA Tools - JSF, Flex, and JavaFX
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
CFDynamics Announces Renewed Agreement with SmarterTools
CFDynamics, a ColdFusion web host, has renewed an agreement with SmarterTools that will allow them to pass on immediate value to their customers. When a customers signs up for a dedicated hosting account they will now receive $750 worth of features including SmarterMail, SmarterStats a
Microsoft's Virtualization Chief Mike Neil To Keynote SYS-CON's Virtualization Conference & Expo
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
SYS-CON's Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
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