> I'm just not understanding why I'd need a getter and a setter 
> in my cfc. 
> 
> ...
>
> What is the value of keeping each value in its own variables 
> scoped variable? Where does it come in handy? Where are you 
> supposed to use it?

As many people have mentioned, you don't need getters and setters. However,
they do provide a potential value.

If you're creating a value object - an object whose sole purpose is to map
to a specific database record, generally - you don't want to embed business
logic in that object. So, you could create something like this:

<!--- contact.cfc --->
<cfcomponent>

        <cfset this.name = "">
        <cfset this.address = "">
        <cfset this.city = "">
        <cfset this.state = "">
        <cfset this.zip = "">

</cfcomponent>

Those variables, being public, could then be manipulated by users of the
value object:

<cfset objContact = CreateObject("component", "path.to.contact")>
<cfset objContact.name = "Dave">
<cfset objContact.address = "1523 16th St NW"> ...

And, honestly, in many environments that might be sufficient. However, what
if you want to provide basic validation within your value object? Or what if
you want to ensure that the values you retrieve from your value object are
the right datatype? You won't be able to do that with public variables;
instead, you'd need getters and setters:

<cfcomponent>

        <cfset variables.name = "">
        <cfset variables.address = "">
        <cfset variables.city = "">
        <cfset variables.state = "">
        <cfset variables.zip = "">

        <cffunction name="getName" access="public" returntype="string">
                <cfreturn variables.name>
        </cffunction>

        <cffunction name="setName" access="public" returntype="boolean">
                <cfargument name="name" required="true" type="string">
                <cfif Len(arguments.name) lt 5>
                        <cfthrow type="contact.validationError"
                                message="Name is too short!"
                                detail="Name argument must be five
characters or more.">
                </cfif>
                <cfset variables.name = arguments.name>
        </cffunction>

        ...

</cfcomponent>

In the above example, the object is in charge of setting its own internal
values, and can validate inputs to do so.

Now, honestly, in many cases people don't bother doing that, or there aren't
any meaningful validation tests to perform, so people build the simplest
possible getters and setters, and they don't do anything beyond providing
encapsulation. But still, this can be valuable, as you can add validation at
any time without breaking the use of those components.

The value of getters and setters is a little more obvious in languages like
Java and ActionScript 3, which let you provide method names that can be
invoked as if they were the actual properties themselves:

private var name:String;

private function set name(newName):String
{
        name = newName;
}

....

myObj.name = "Dave"; // this would call the method described above

If you want to provide the encapsulation, but don't want to bother writing
the code, there are a couple of approaches you can take. In CF8, you can use
onMissingMethod to write one generic method that would respond to calls for
specific getters and setters that don't actually exist. Or, better yet (in
my own personal opinion) you could use one of the many, many code generators
that let you point to a database table and generate value objects (and DTOs,
gateway/assembler objects, etc). Since you can use these to reduce your time
spent to nearly zero, there isn't much reason not to.

Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/

Fig Leaf Software provides the highest caliber vendor-authorized
instruction at our training centers in Washington DC, Atlanta,
Chicago, Baltimore, Northern Virginia, or on-site at your location.
Visit http://training.figleaf.com/ for more information!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to 
date
Get the Free Trial
http://ad.doubleclick.net/clk;203748912;27390454;j

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:308010
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to