Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

getters and setters

2 views
Skip to first unread message

Mantalore

unread,
Mar 7, 2005, 6:15:12 PM3/7/05
to
I am new to CFCs and I think I am missing the point of getters and setters A
lot of the text says that when writing a cfc we should write getter and setter
methods but it seems like a lot of extra work and I would like to know what the
benefits are. Why is it not a good idea to set the variables publicly and
access the cfc properties directly? I produced the following cfc:
<cfcomponent hint='I am the son' extends='Dad'> <cfset this.firstName='Rob'>
<cffunction name='getfirstname' returntype='string'> <cfreturn
this.firstName> </cffunction> </cfcomponent> Then I produced the following
test page: <cfobject component='Son' name='sonObj'> <cfoutput>
#sonObj.firstname#<br> #sonObj.getfirstname()#<br> <cfset
sonObj.firstname='Adam'> #sonObj.firstname#<br> #sonObj.getfirstname()#<br>
</cfoutput> Which produced the following output: Rob Rob Adam Adam I
realise that by scoping the firstname with 'this' that I am making it a public
variable which can be accesssed outside the object and that the benifit of
encapsulation is not gained but my question is why is this not done in the
examples of the web? By scoping the property in the 'variables' scope and
writing getters &amp; setters we may get encapsulation but the ability of being
able to change properties remains via the getters &amp; setters. Is it because
setters allow for validation that setting the value publicly does not? But if
so why write getters, they dont require validation. If variables are set
publicly are they open problems that I am not aware of?If so could some one
show me an example. Are there any good articles on the subject?

maybenull

unread,
Mar 7, 2005, 11:52:59 PM3/7/05
to
two reasons stand out to me on why to use getters/setters in coldfusion
1) validation/behavior (you already mentioned this of course) - its covered
well at http://naeblis.cx/rtomayko/2005/01/20/getters-setters-fuxors where
python, which handles this irksum pattern well, is compared to java.
2) tool/utility support - many tools automagically call getters and setters
for you, machii event-bean comes to mind; as for the coding of the getters and
setters, most good ide's, such as eclipse and intellij idea, will generate them
for each variable in your component. no coldfusion ide has reached this point
to my knowledge, but i do have an issue entered for this in cfeclipse.

0 new messages