Introducing cfinterface
I am a little surprised that I have not heard too much from others about the new cfinterface tag. There has been some debate within the community about the usefulness of interfaces in dynamic languages such as ColdFusion. This article will not cover that debate. A quick search in your favorite search engine will give you some very good articles. One article that I found educational was an article by Brandon Harper at I figured, since it is probably going to be included in the release of Scorpio (ColdFusion 8), I thought I would educate others about it.

What Is An Interface?
An interface is an abstract component that defines a set of methods that must be defined when it is implemented. As interfaces are abstract, they cannot be directly instantiated. If you try to instantiate the interface, you will receive the following error, “Cannot create interface“. This will let you know that are trying to directly instantiate an interface, which is frowned upon. If you try and instantiate a component that implements a interface and does not define all of the methods required, you will get the following error, “The [methodname] method is not implemented by the component or it is declared as private“. Interfaces provide a contract between the interface and any component that implements it. ColdFusion will make sure that contract is fulfilled at run-time.

<cfinterface> tag
The following information is from the cfml reference documentation. cfinterface: Defines an interface that consists of a set of signatures for functions. The interface does not include the full function definitions; instead, you implement the functions in a ColdFusion component (CFC). The interfaces that you define by using this tag can make up the structure of a reusable application framework. You define an interface by creating a ColdFusion file with a .cfc suffix and specifying the cfinterface tag as the first and only top-level tag in the file. The cfinterface tag has three attributes all of which are optional.

  • displayname – A value to be displayed when using introspection to show a descriptive name for the interface.
  • extends – A comma-delimited list of one or more interfaces that this interface extends. Any CFC that implements an interface must also implement all the functions in the interfaces specified by this property. If an interface extends another interface, and the child interface specifies a function with the same name as one in the parent interface, both functions must have the same attributes; otherwise ColdFusion generates an error.
  • hint – Text to be displayed when using introspection to show information about the interface. The hint attribute value follows the syntax line in the function description.

Show Me The Money (code).
Starting to make a little sense to you, but you want to see some code? OK, here you go! In my example, I will have four components. The first component is our interface, named ShippingPricing. The ShippingPricing interface has one method called calcShippingCost(). It is also good coding standards practice to prefix an interface name with an I.


Next, we have three components that will all implement the ShippingPricing interface. This means that they must all provide an implementation of the calcShippingCost() method. Each type of shipping service has a formula for calculating the price of shipping. This approach is a scalable solution and much cleaner than sticking 3 methods all in one component.




Finally we will use our test template to instantiate the components. Please remember we do not create a component by instantiating our Interface rather we call the service that we need.

cfInterface Testing

Why use Interfaces in ColdFusion?
Interfaces provide us with another tool in our Object Oriented Programming tool belt. When you start to research different design patterns that are proven to solve certain problems you will run across an interface sooner or later. We now have the power to use these design patterns. While the debate on weather they are needed will continue somebody up there thinks it is a nice addition and I happen to agree. I hope this gave you a little preview of the new interface functionality in Scorpio. While some people will argue that interfaces are useless, they will be included in ColdFusion 8, and I intend to take advantage of them.

Categories: ColdFusion

About The Author

My name is Dan Vega and I am a Software Developer based out of Cleveland OH. I love to play with new technologies and write about my experiences here. When I am not busy being a full time geek I love to lift heavy weights and hang out with friends and family. If you have any questions please don't hesitate to contact me.

Follow me on:
  • David Beale

    Great Article. I for one will definitely be using cfinterface!

  • Anang

    I don’t still see where you have used the interface.
    Instead of …
    <cfset groundService = createObject("component","org.danvega.GroundService")>
    <cfset twoDayService = createObject("component","org.danvega.TwoDayService")>
    <cfset overnightService = createObject("component","org.danvega.OvernightService")>

    How will this logic change if you did not have that interface in there ?
    I am not debating… I need help in doing some custom validation. I thought interface would be nice to have in the following scenario:
    +— EmailRegExpValidator: implements IValidator
    +— WebSiteRegExpValidator: implements IValidator
    +— CustomStringValidator: implements IValidator

    +— Application1Users : extends UserContactInfo
    +— Application2Users : extends UserContactInfo

    UserContactInfo HAS A IValidator

    — Stumped

  • Andy Matthews

    I’m confused about your explanation. I’m trying to decide whether to use interfaces or not. First off, you create an "IShippingPricing.cfc", then never reference that anywhere else. Then you have ‘implements="IShippingService"’ in each of your actual CFCs (which I assume is meant to be IShippingPricing instead. I’m sure that’s just an oversight though.

    The main question I have is what’s the point of even implementing an interface that has no code in it. Like the previous poster Anang asked. Why even have the interface if it doesn’t have any code, and doesn’t appear to do anything?

    As Anang said, I’m not debating…I really want to learn, but your example makes me think that cfinterface isn’t a good choice for my app.

  • doug boude

    Hi Dan. I realize this is an older post, but it’s new to me! I just wanted to comment on another usage of interfaces that I’ve encountered with CF, and that is as type values in cfargument tags or returntype attributes. Seems for performance reasons most people tend to set type attributes to "any", but you can create an interface for use in type validation as well.

  • Dan Vega

    I understand why people use any but can you expand on why you would use it in type validation? Blog post coming soon from you?