|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS Foundations
Mixins
Writing software that's more flexible and maintainable
By: Hal Helms
Mar. 11, 2006 02:00 PM
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..
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 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{ 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:
public class Teacher implements interfaces.Teacher{ Now, I'll implement the Teacher interface in another class - say, StudentTeacher:
public class StudentTeacher extends Student implements Teacher{ 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 /> This is the code we're going to mix into classes. Our first class is Teacher.cfc:
<cfcomponent displayname="Teacher" extends="BaseComponent"> Page 1 of 3 next page » CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||