Dynamic class casting (yes, again...)

M

Mike Nishizawa

Since most of the threads on this subject deal with how to basically
get around dynamic casting, maybe you guys can help me get around
this.

My structure is this, I have a GenericBean() class and several
SpecificBean() classes. Each of the SpecificBean() classes has
different methods and does not generally override the methods it
inherits from GenericBean(). My problem is this, I am trying NOT to
have to specifically cast the beans for each specific case. The
application flows as such:

Homepage -> menuitem calls a specific form -> the form is prepared by
a java class in which the bean that the form will use is assigned and
put on the request -> form is presented to the user.

Right now, step 3 has to have a new class created every time we add a
new form. I would like to make this 1 class which can take a class
name as an argument and instantiate the right bean. Obviously, just
calling methods from the classes will not work because the bean object
needs to go on the request.

Thanks for your help!
 
T

Tony Morris

Mike Nishizawa said:
Since most of the threads on this subject deal with how to basically
get around dynamic casting, maybe you guys can help me get around
this.

My structure is this, I have a GenericBean() class and several
SpecificBean() classes. Each of the SpecificBean() classes has
different methods and does not generally override the methods it
inherits from GenericBean(). My problem is this, I am trying NOT to
have to specifically cast the beans for each specific case. The
application flows as such:

This is difficult to follow, and I suspect, incomplete.
A test case demonstrating what you are trying to say ?
Are you aware that a cast (and thus, the reference type) does not have any
effect on which class' method is bound at run-time ?
It is the object type who's method is bound, not the reference type.
That's all I can deduce from the above explanation.
Homepage -> menuitem calls a specific form -> the form is prepared by
a java class in which the bean that the form will use is assigned and
put on the request -> form is presented to the user.

Right now, step 3 has to have a new class created every time we add a
new form. I would like to make this 1 class which can take a class
name as an argument and instantiate the right bean. Obviously, just
calling methods from the classes will not work because the bean object
needs to go on the request.

Sounds like you might be in need of implementing the Factory Design Pattern
(or the Abstract Factory).
Thanks for your help!



--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
M

Mike Nishizawa

I'll do my best to clarify with code:

public class GenericBean() {
public String getFirstName() {// implied return of stored value}
public String setFirstName() {// implied set of internal variable}
}

//bean for address
public class SpecificBean1() extends GenericBean {
public String getAddress() {}
public String setAddress() {}
}

//bean for personal info
public class SpecificBean2() extends GenericBean {
public String getSSNumber() {}
public String setSSNumber() {}
}

Then I would of course have 2 pages that use the beans, one for
address info and one for personal info. Both use the firstName field
so I extend the specific beans from the generic bean. On entering the
form, I run a method that assigns the correct bean and this method is
always run when refreshing the form so the information is always
stored in my bean. Right now I have one of these for every form. I
want to make it more dynamic so I only have to use one to cover all
cases. So right now I would have:

public void preparePersonalForm(GenericBean genBean) {
SpecificBean2 sb2 = (SpecificBean2)genBean;
request.setAttribute("theBean", sb2);
}

I would like it to be something like this(I realize that this is not
going to work):

public void prepareForm(GenericBean genBean, Class theClass) {
theClass mySpecificBean = (theClass)genBean;
request.setAttribute("theBean", mySpecificBean);
}


Thanks again for the help.
 
M

Michiel Konstapel

Right now I have one of these for every form. I
want to make it more dynamic so I only have to use one to cover all
cases. So right now I would have:

public void preparePersonalForm(GenericBean genBean) {
SpecificBean2 sb2 = (SpecificBean2)genBean;
request.setAttribute("theBean", sb2);
}

I would like it to be something like this(I realize that this is not
going to work):

public void prepareForm(GenericBean genBean, Class theClass) {
theClass mySpecificBean = (theClass)genBean;
request.setAttribute("theBean", mySpecificBean);
}

Why bother with the casts at all? request.setAttribute() takes an Object
for the value, so there's no need to cast anything. Just use

public void prepareForm(GenericBean genBean) {
request.setAttribute("theBean", genBean);
}

If it was instantiated as a SpecificBean, it'll still be one. A cast
doesn't change that and the map inside holding the request attributes
doesn't care, it's all Objects it sees.
HTH,
Michiel
 
S

S Manohar

public void preparePersonalForm(GenericBean genBean) {
SpecificBean2 sb2 = (SpecificBean2)genBean;
request.setAttribute("theBean", sb2);
}

I would like it to be something like this(I realize that this is not
going to work):

public void prepareForm(GenericBean genBean, Class theClass) {
theClass mySpecificBean = (theClass)genBean;
request.setAttribute("theBean", mySpecificBean);
}


Thanks again for the help.

Why do you need the cast at all? The cast does not alter the _value_
being passed to setAttribute(), only its type. Can't you simply call
request.setAttribute("theBean",genBean); ?

(Unless, that is, the setAttribute method is actually overloaded for
the various types of GenericBean: I think a method signature's
parameter types are bound at compile time not run time.)

It would be different if you needed to explicitly access specific
methods of SpecificBeans, in which case you'd definitely need explicit
casts (and type checks).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top