Person class just stores data, it has no behaviour. Known as a Value Object (VO) or Data Transfer Object (TDO)
<cfset forename = "Jane" /> <cfset surname = "Smith" /> <cfset dob = CreateDate( 1970, 6, 25 ) /> <cfset Person = CreateObject( "component", "Person" ).init( forename, surname, dob ) /> <cfoutput> <p> Name: #Person.getFullName()#<br /> Age: #Person.getAgeInYears()#<br /> </p> </cfoutput> <p><strong>Jane gets married to Mr Jones</strong></p> <cfset Person.setLastName( "Jones" ) /> <cfoutput> <p> Name: #Person.getFullName()#<br /> Age: #Person.getAgeInYears()# </p> </cfoutput>
<cfcomponent displayname="I am an example of a Value Object" output="false"> hint="I have data and behaviour" <!--- =================================================================== Public Methods =================================================================== ---> <cffunction name="init" access="public" returntype="any" hint="I act as the constructor"> <cfargument name="firstname" type="string" required="yes" /> <cfargument name="lastname" type="string" required="yes" /> <cfargument name="dateOfBirth" type="date" required="yes" /> <!--- Create a structure to hold our instance variables in. This makes dumping instance variables out easy. ---> <cfset variables.instance = StructNew() /> <!--- Use the setters instead of setting the variable directly ---> <cfset setFirstname( arguments.firstname ) /> <cfset setLastname( arguments.lastname ) /> <cfset setDateOfBirth( arguments.dateOfBirth ) /> <cfreturn this /> </cffunction> <!--- ************************************************* Getters and Setters ************************************************* ---> <cffunction name="getFirstname" access="public" output="false" returntype="string" hint="I return the value of firstname"> <cfreturn variables.instance.firstname /> </cffunction> <cffunction name="setFirstname" access="public" output="false" returntype="void" hint="I set the value of firstname"> <cfargument name="firstname" required="true" type="string" hint="I am the new value of firstname" /> <cfset variables.instance.firstname = arguments.firstname /> </cffunction> <cffunction name="getLastname" access="public" output="false" returntype="string" hint="I return the value of lastname"> <cfreturn variables.instance.lastname /> </cffunction> <cffunction name="setLastname" access="public" output="false" returntype="void" hint="I set the value of lastname"> <cfargument name="lastname" required="true" type="string" hint="I am the new value of lastname" /> <cfset variables.instance.lastname = arguments.lastname /> </cffunction> <cffunction name="getDateOfBirth" access="public" output="false" returntype="date" hint="I return the value of dateOfBirth"> <cfreturn variables.instance.dateOfBirth /> </cffunction> <cffunction name="setDateOfBirth" access="public" output="false" returntype="void" hint="I set the value of dateOfBirth"> <cfargument name="dateOfBirth" required="true" type="date" hint="I am the new value of dateOfBirth" /> <cfset variables.instance.dateOfBirth = arguments.dateOfBirth /> </cffunction> <!--- ************************************************* Give our class some behaviour! ************************************************* ---> <cffunction name="getAgeInYears" access="public" output="false" returntype="numeric" hint="I return the age in years"> <cfreturn DateDiff( "yyyy", variables.instance.dateOfBirth, Now() ) /> </cffunction> <cffunction name="getFullname" access="public" output="false" returntype="string" hint="I return the name for consistent display"> <cfreturn getFirstname() & " " & getLastname() /> </cffunction> </cfcomponent>