Value Object example

Person class just stores data, it has no behaviour. Known as a Value Object (VO) or Data Transfer Object (TDO)


View The Results...

index.cfm
<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.getFirstName()# #Person.getLastName()#<br />
Age: #DateDiff( "yyyy", Person.getDateOfBirth(), Now() )#<br />
</p>
</cfoutput>

<p><strong>Jane gets married to Mr Jones</strong></p>

<cfset Person.setLastName( "Jones" ) />

<cfoutput>
<p>
Name: #Person.getFirstName()# #Person.getLastName()#<br />
Age: #DateDiff( "yyyy", Person.getDateOfBirth(), Now() )#
</p>
</cfoutput>
Person.cfc
<cfcomponent 
	displayname="I am an example of a Value Object" 
	output="false">
	hint="I am basically just a glorified struct with no 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 (available to all methods of this class)
		This makes dumping instance variables out easy and makes code more readable.
		---> 
		<cfset variables.instance = StructNew() />
	
		<!---
		Use the setters instead of setting the variable directly.
		We do this so that change only happens in one place,
		for example, we want to capitalise the first letter of firstname and surname
		--->
		<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>
	
</cfcomponent>

View The Code...

Name: Jane Smith
Age: 38

Jane gets married to Mr Jones

Name: Jane Jones
Age: 38


« Part 1 Menu