> we are creating beans and have noticed in tutorials that some 
> people use <cfset variables.instance = structnew()> and then 
> start placing all the variables inside this 
> variables.instance structure.
> 
> however if we build up the cfc through the cfeclipse cfc 
> builder then it creates all variables as properties using the 
> cfproperty tag
> 
> is there are difference or is this just the same way of doing 
> the same thing. if so then is there a preferred method and why?

The CFPROPERTY tag doesn't actually create properties. It simply provides
documentation for properties, so that they'll show up in the
automatically-generated documentation.

Using an "instance" structure is really just a convenience for the
developer, to let you easily iterate through and dump the instance data. It
doesn't really provide any value to the program's functionality.

Using the Variables scope makes those variables private, so that they can't
be directly read or changed from outside the component instance. This is
generally the preferred way to build objects, because objects are supposed
to hide and control their internals, and only expose functionality through
methods. This is called "encapsulation", and it's a fundamental concept of
OO programming.

However, this doesn't always work as smoothly as you might like in CF,
because CF doesn't work exactly like Java or AS3 or any number of OO
languages. In Java, when you create a bean, the bean can have private
properties, and you can access those properties through accessor and mutator
methods ("getters" and "setters"), and those methods will look like
properties from within the program manipulating the bean. So, when you have
a line like this:

myBean.someProperty = "someValue";

that line would actually be calling a method instead of setting a property.

CF doesn't have this functionality. You can write getters and setters, but
you'll have to call them as you would any other method. So, there's a bit of
tension between the concept of encapsulation and the bean pattern. Frankly,
I don't know if I'd say there's a "right" way here, you should choose
whichever concept you think is more important in expressing the
functionality you want. For example, the bean generator in Flex Builder
generates public properties (even though AS3 allows you to build getters and
setters that can be invoked as if they were properties), and I don't bother
to change the generated code for two reasons: first, it lets code completion
work better, because Flex Builder can use introspection, and second, I don't
generally manipulate the beans directly much anyway, because the beans are
generated and manipulated through abstraction layers that I don't have to
tinker with directly. Now, the first reason doesn't apply, I suppose,
because we don't have that kind of introspection in CF editors since there's
no compiler involved in the editing process, but the second might be
applicable to you.

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:312369
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to