Dynamic class casting (yes, again...)

Discussion in 'Java' started by Mike Nishizawa, Mar 3, 2004.

  1. 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!
    Mike Nishizawa, Mar 3, 2004
    #1
    1. Advertising

  2. Mike Nishizawa

    Tony Morris Guest

    "Mike Nishizawa" <> wrote in message
    news:...
    > 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
    Tony Morris, Mar 3, 2004
    #2
    1. Advertising

  3. 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:...
    > > 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!
    Mike Nishizawa, Mar 4, 2004
    #3
  4. > 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
    Michiel Konstapel, Mar 5, 2004
    #4
  5. Mike Nishizawa

    S Manohar Guest

    > 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).
    S Manohar, Mar 5, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. u.Shanker

    Dynamic Casting again with array

    u.Shanker, Aug 25, 2004, in forum: Java
    Replies:
    1
    Views:
    1,961
    John C. Bollinger
    Aug 26, 2004
  2. teo
    Replies:
    3
    Views:
    533
  3. Jamie Herre
    Replies:
    1
    Views:
    194
    why the lucky stiff
    Jan 7, 2005
  4. Josh Hester
    Replies:
    1
    Views:
    103
    Logan Capaldo
    Sep 12, 2007
  5. Replies:
    11
    Views:
    226
Loading...

Share This Page