Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Dynamic class casting (yes, again...)

Reply
Thread Tools

Dynamic class casting (yes, again...)

 
 
Mike Nishizawa
Guest
Posts: n/a
 
      03-03-2004
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!
 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      03-03-2004

"Mike Nishizawa" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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


 
Reply With Quote
 
 
 
 
Mike Nishizawa
Guest
Posts: n/a
 
      03-04-2004
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.


"Tony Morris" <(E-Mail Removed)> wrote in message news:<c25m17$bs2$(E-Mail Removed)>...
> "Mike Nishizawa" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) m...
> > 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!

 
Reply With Quote
 
Michiel Konstapel
Guest
Posts: n/a
 
      03-05-2004
> 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
 
Reply With Quote
 
S Manohar
Guest
Posts: n/a
 
      03-05-2004
> 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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
Getting class using (Class.forname()) and casting the instance Madni Java 1 12-19-2005 07:00 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM



Advertisments