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 http://devnulled.com/tag/java/page/2/. 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.
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.
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.