Skip to content

Instantly share code, notes, and snippets.

@djleeds
Last active December 17, 2015 01:39
Show Gist options
  • Save djleeds/5529833 to your computer and use it in GitHub Desktop.
Save djleeds/5529833 to your computer and use it in GitHub Desktop.
Demonstrates inheritance, composition, mixins, and patches in ColdFusion. The article that corresponds to these code snippets can be found here: http://www.hitthebits.com/2013/05/coldfusion-code-reuse.html
<!--- EggLayer.cfc --->
<cfcomponent>
<cffunction name="layEggs">
<cfargument name="count" />
<cfreturn "I just laid #count# eggs." />
</cffunction>
</cfcomponent>
<!--- Duck.cfc and Platypus.cfc --->
<cfcomponent extends="EggLayer">
<!--- ...elided... --->
</cfcomponent>
<!--- Duck.cfc and Platypus.cfc --->
<cfcomponent>
<cfset variables.eggLayer = new EggLayer() />
<cffunction name="layEggs">
<cfargument name="count" />
<cfreturn variables.eggLayer.layEggs(arguments.count) />
</cffunction>
</cfcomponent>
<!--- eggLayer.cfm --->
<cffunction name="layEggs">
<cfargument name="count" />
<cfreturn "I just laid #count# eggs." />
</cffunction>
<!--- Duck.cfc and Platypus.cfc --->
<cfcomponent>
<cfinclude template="eggLayer.cfm" />
<!--- the rest of the class is elided --->
</cfcomponent>
<!--- Duck.cfc and Platypus.cfc --->
<cfcomponent>
<!--- No change to these files is required --->
</cfcomponent>
<!--- index.cfm, or maybe some factory class --->
<cffunction name="layEggs">
<cfargument name="count" />
<cfreturn "I just laid #count# eggs." />
</cffunction>
<cfset duck = new Duck() />
<cfset platypus = new Platypus() />
<cfset duck.layEggs = this.layEggs />
<cfset platypus.layEggs = this.layEggs />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment