Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 00:37
Mom! Ben Broke My CFInterface!
<cfinterface
hint="Defines the interface for a dog.">
<cffunction
name="Eat"
access="public"
returntype="void"
output="false"
hint="Allows the dog to eat the given food.">
<cfargument
name="Food"
type="any"
required="true"
hint="The food to be eaten."
/>
</cffunction>
<cffunction
name="Poop"
access="public"
returntype="string"
output="false"
hint="Returns some poop.">
</cffunction>
</cfinterface>
<cfcomponent
implements="IDog"
output="false"
hint="Implements the dog actions for the Lhasa Apso.">
<cffunction
name="Eat"
access="public"
returntype="void"
output="false"
hint="Allows the dog to eat the given food.">
<cfargument
name="Food"
type="any"
required="true"
hint="The food to be eaten."
/>
<!---
You eating algorithm would be here; but, for
this demo, we are not going to worry about how
things actually work.
--->
<!--- Return out. --->
<cfreturn />
</cffunction>
<cffunction
name="Poop"
access="public"
returntype="string"
output="false"
hint="Returns some poop.">
<!--- Return the poop. --->
<cfreturn "poop" />
</cffunction>
</cfcomponent>
<!---
Create the Lhasa Apso dog (which implements the
dog interface).
--->
<cfset objDog = CreateObject( "component", "LhasaApso" ) />
<!--- Feed the dog. --->
<cfset objDog.Eat( "T-Bone Steak" ) />
<!--- Poop the dog. --->
#objDog.Poop()#<br />
<br />
<br />
<!---
Now that we have demonstrated that the Dog
works, let's define a random function that
returns some stuff and see if it works
with the dog.
--->
<cffunction
name="Talk"
access="public"
returntype="string"
output="false"
hint="Says something">
<!--- Return some speaking stuff. --->
<cfreturn "I like the way you look in that dress." />
</cffunction>
<!--- Assign the function to the instantiated dog. --->
<cfset objDog.Talk = VARIABLES.Talk />
<!--- Feed the dog. --->
<cfset objDog.Eat( "Porter House Steak" ) />
<!--- Poop the dog. --->
#objDog.Poop()#<br />
<!--- Converse with the dog. --->
#objDog.Talk()#<br />
<!--- Take away the ability for the dog to eat. --->
<cfset StructDelete( objDog, "Eat" ) />
<!--- Try to feed it now. --->
<cfset objDog.Eat( "Roasted Chicken" ) />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment