getters and setters

getters and setters

Post by Mantalor » Wed, 09 Mar 2005 08:15:12

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?

getters and setters

Post by maybenul » Wed, 09 Mar 2005 13:52:59

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 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.


1. getters and setters question


i am reading the book: 'object oriented programming  with actionscript 2'
by jeff tapper, james talbot and robin haffner.

they explain the working of explicit getter and setter methods.

this is an example they use:

class TextControl extends TextField{
    function TextControl(){

 public function setText(textToSet: String) : Void{
  this.theText.autoSize = true;
  this.theText.text = textToSet;

 public function getText() : String{
   return this.theText.text;

in the fla file there is an mc called 'myText' and in the mc an textfield
called 'theText';

this is the line they use to populate the textfield with a method:
myText.setText("some text");

my question is this:
- how is is possible that i see the text in the textfield because the
function/method getText() is not invoked.
only the function/method setText is invoked?
- how is it possible that the codelines in the function/method getText are
- how does the programme gets from 'setText()' to 'getText'?


madame b

2. Is Flash for me?

3. Getters and Setters addproperty component question?

4. This is odd! What is a 'color table'?

5. When to use getters/setters with CFC's

6. IE 4.0 and readextfile

7. CFC Getters

8. passing variables with a url (not in the url though)