implicit accessors and inheritance

41 views
Skip to first unread message

garence

unread,
Dec 23, 2011, 7:59:36 AM12/23/11
to Object-Oriented Programming in ColdFusion
Hi all -

Could someone help me with what inheritance might look like for two
beans that use implicit accessors?

As an example, I have a User bean as follows:

component displayname="User" accessors="true" output=false {

property name="firstName" type="string";
property name="lastName" type="string";

public model.beans.User function init(string firstName='', string
lastName='') output=false {
setFirstName(arguments.firstName);
setLastName(arguments.lastName);
return this;
}

public string function getFullName() output=false {
var str=getFirstName() & ' ' & getLastName();
return str;
}

}

I have a Player bean (below) that I want to extend my User bean:

component displayname="Player" extends="model.beans.User"
accessors="true" output=false {

property name="jerseyNumber" type="string";

public model.beans.Player function init(string jerseyNumber='')
output=false {
setJerseyNumber(arguments.jerseyNumber);
return this;
}

}

My question is, how do I modify this Player.cfc accordingly?

Much thanks for any help here.

Gary

Denard Springle

unread,
Dec 27, 2011, 10:17:25 AM12/27/11
to Object-Oriented Programming in ColdFusion
Hey Gary,

The same way you do it without implicit accessors - with
super.init()

e.g. in your player init() you need to super.init(firstName = 'bob',
lastName = 'jones'), for example:

component displayname="Player" extends="model.beans.User"
accessors="true" output=false {

property name="jerseyNumber" type="string";

public model.beans.Player function init(string jerseyNumber='')
output=false {
<!--- SUPER THE BASE CLASS --->
super.init(firstName = 'John', lastName = 'Denver');
setJerseyNumber(arguments.jerseyNumber);
return this;
}

}

Then you can access the player function to getFirstName() and
getLastName() as well as getJerseyNumber().

Hope this helps and sorry for the late response - busy time of year ;)

-- Denny

Denard Springle

unread,
Dec 27, 2011, 10:43:39 PM12/27/11
to Object-Oriented Programming in ColdFusion
Hey Gary,

Sorry, my last example was incomplete - you would also need to have
those base class values in your Player init() (see more complete
example below).

If you're using implicit accessors anyway, you can skip the whole
init() routine altogether and not have to call super - these two
classes would wire together perfectly with the Player class extending
the base class and both containing only accessors="true" and the
properties (see second example below).

If you're using 9.0.1 you can take advantage of mappedSuperClass to
map a (non-persistent) base class to persistent child classes to share
functionality (see 3rd example below)

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7e0e.html
links to the cfcomponent tag in the CF9 livedocs - it's a good read
and will explain more about the options available to you. There's also
a great deal of info in the developers guide for 9 that speaks to this
topic and might also be a good read. Also, Object-Oriented Programming
in ColdFusion book has a small subset on inheritance and the use of
super.

In any case, the following ought to work even with implicit accessors
(though I didn't test it, so don't hold me to it):

component displayname="Player" extends="model.beans.User"
accessors="true" output=false {

property name="jerseyNumber" type="string";

public model.beans.Player function init(string jerseyNumber=',
string firstName='', string lastName=''')
output=false {
<!--- SUPER THE BASE CLASS --->
super.init(firstName = arguments.firstName, lastName =
arguments.lastName);
setJerseyNumber(arguments.jerseyNumber);
return this;
}

}

And the alternative should be something along the lines of:

component displayname="User" accessors="true" output=false {

property name="firstName" type="string";
property name="lastName" type="string";

}

component displayname="Player" extends="model.beans.User"
accessors="true" output=false {

property name="jerseyNumber" type="string";

}

Calling Player.getFirstName() will return the value stored with a
Player.setFirstName(). No init() needed.

And, finally, using a non-persistent (not ORM) base class with a
persistent (ORM + accessors = true) class:

component displayname="Player" extends="model.beans.User"
accessors="true" mappedSuperClass="true" output=false {

property name="jerseyNumber" type="string";

}

In which case your base class should not be persistent (accessors =
false) - CF will create the accessors and wire them to any components
which share the base class when using mappedSuperClass="true" with an
ORM (persistent) application.

Hope this is more helpful - was a bit rushed unexpectedly earlier and
wanted to circle back and try and clarify the options ;)

-- Denny
Reply all
Reply to author
Forward
0 new messages