Aaron,
I'm not familiar with the code you are referring to, but this is
generally done to encapsulate the data within the object, and manage
access to that data. This allows you to alter / update how you manage
that data internally without affecting any code that relies on the
object itself, thus reducing the dependencies between your objects.
In your code example, assume that the code currently just takes the
title as typed in from the form and directly writes it to the database.
What happens when you realize that people are putting invalid characters
into the form? Would you message the data before passing it into the
object, or have the object itself ensure the data is correct? If you
exposed the variables without accessors then you would have no choice
but to message the data externally and probably in many different places
that might access this object. Using accessors you could simply message
the data internally (or in a separate object meant strictly for this
type of work) and then store it.
I hope that helps,
Rich
----------
<cfscript>
article.setTitle(form.title);
article.setTeaser(form.teaser);
article.setBody(form.body);
article.setAuthor(form.author);
if (article.getID() eq 0) {
article.create();
} else {
article.update();
}
</cfscript>
Is it wise/necessary/required to create a set method
for every parameter that is passed to the method?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:241727
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54