Joe,
I didn't have the time to read all of this, but I thought I might answer
a couple of the questions that I have read. First, to get a good
background in OO, there are plenty of resources available. I'd check
out a book on the subject for sure. Also, I always like to point people
to Bob Martin's site, where he lists the principles of OO design. Those
really helped stuff click for me.
http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod ... also, look
up cohesion and coupling and understand them. If you need, you might
want to read more basic things about the terminology, but so far you
sound like you've probably done that ... More follows...
Joe Lakey wrote, On 3/6/2007 2:08 PM:
I've started reading some of the MVC stuff on the web, but it's still a
little above my head. I'm really having trouble differentiating between
the model and controller layers.
The model is where you put the "business logic" (also commonly called
domain logic) of your application. For instance, if you were doing a
shopping cart, you'd want to put classes like Product, Customer, etc in
there, and methods that know how to calculate the total of an order,
save orders, and stuff like that.
The controller does just what it says- it controls the application. It
wires together the view and the model, so to speak. This is where you
might put authentication checks like <if isAuthenticated() show admin
stuff>. That's really a bad way to describe it, but you'd put the
knowledge of how to do the authentication in the model layer (probably)
and put the actual authentication in a controller. I suppose you could
also put it in the model, but that, to me, would be a lot more hassle
and doesn't follow logically (although others may differ in their
opinion). Basically, a control would route requests and let the view
have the information it needs from the model. That's how I look at it.
2. If a CFC extends another CFC, does the init() method need to call
Super.init()? If so, why doesn't this create two instances--one of the
"parent" CFC and one of the child CFC? Does super.init() automatically
receive arguments passed to init(), or do they have to be explicitly
passed to super.init()?
If you redefine the init method, no, it won't call it unless you also
call super.init(). And no, it wouldn't automatically receive any
arguments. Init is just a convention that was established to get
information into your CFCs, as there is no real constructor to call to
do that. Therefore, it would behave as any other method.
3. If a method overrides a method of a base CFC, does the child method
need to to call super.myMethod()?
Not unless you want to perform the "super" functionality in addition to
your overriding of it. This is bad example, but the only one I have in
my head right now: suppose in Base.cfc you have a method like
displayDiv. Then, in Derived.cfc you want that method that would wrap
that div in a box. You could have something like
<cffunction name="displayDiv" ....>
<div style="border: 1px solid black;">
super.displayDiv() // this will set whatever was being done originally
</div>
</cffunction>
Of course, this is a poor example, but my mind is drawing blanks right now.
What if the child method is only
intended to override part of the parent method's functionality? For
instance, the method may set a number of properties, but the child
method only needs to override some of those properties.
Well, you'll either need to factor better, or override all the
properties. I'd prefer to factor better. There are also patterns that
you might want to look up after gaining some experience. In particular,
there is one that might apply here. I can't think of the name though.
I'm not thinking strategy, but something else (strategy is similar in
intent to what I'm thinking,... anyone want to jump in and help me?)
Also, similar to
question 2, does super.myMethod() receive arguments passed to
myMethod(), or must they be explicitly passed to super.myMethod()?
No, again, you'll need to pass it.
4. How is a method of one object made available to the methods of
another object?
You can either use composition/aggregation (in which the object you are
trying to reuse would be contained within your object), or you can use
inheritance, as you've mentioned above. Most of the time, you'll
probably be using containment. Inheritance is generally only advised
for when what you're doing is really inheritance.
Can a property of the second object be a reference to
the first object?
It can, but generally you don't do that. You'd provide getters and
setters (also called accessors and mutators by some) to do that for the
properties you want others to be able to edit. This deals with
encapsulation (but encapsulation is much more than just that). If you
use the "this" scope on these properties, then you can access them.
Most people advise against that.
If so, is the first object actually passed by
reference or by value (by duplication?)?
If it is an object or complex value, I believe it is passed by
reference. Otherwise, I think it is passed by value. But, someone else
would probably know for sure. Incidentally, you could test this out by
trying to change it and seeing if it changes in the original object.
For example, in this code, can firstCfcMethod() call
Variables.SecondCfcReference.secondCfcMethod()?
<cfcomponent displayname="FirstCfc">
<cfproperty name="SecondCfcReference" type="SecondCfc">
<cffunction name="init">
<cfargument name="SecondCfcInstance" type="SecondCfc"
required="yes">
<cfset Variables.SecondCfcReference=Arguments.SecondCfcInstance>
</cffunction>
<cffunction name="firstCfcMethod">
<cfset Variables.SecondCfcReference.secondCfcMethod() >
</cffunction>
</cfcomponent>
<cfcomponent displayname="SecondCfc">
<cffunction name="secondCfcMethod">
<!--- FUNCTION CODE --->
</cffunction>
</cfcomponent>
I don't recall if the default for functions is public or private or
"nothing and i'll let you call it" or "nothing and i won't let you call
it" but I think it is "nothing and I'll let you call it." So, yes, it
can call that function. But, that would be able to happen no matter if
it was passed by reference or passed by value. If you are asking in the
case that secondCFCMethod changes something in secondCFC, then yes, I
think you would see the change in the original object, because i think
it is passed by reference.
Hope that helps. Often, I don't feel like I explain things very well,
and I think this might be one of those times. Feel free to ask for
clarifications though.
-Sam
You are subscribed to cfcdev. To unsubscribe, please follow the instructions at
http://www.cfczone.org/listserv.cfm
CFCDev is supported by:
Katapult Media, Inc.
We are cool code geeks looking for fun projects to rock!
www.katapultmedia.com
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]