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" <> wrote in message news:<c25m17$bs2$>...
> "Mike Nishizawa" <> wrote in message
> news: 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!
|