Pattern for recursive variable setting over type hierarchy

Discussion in 'Java' started by John McC, Aug 20, 2003.

  1. John McC

    John McC Guest

    my program needs to set many variables posted from a very complex form
    by working its way up a multi-tiered type hierarchy
    and setting the relevant variables that apply at that level. ie:
    Class A - variables aX,aY,aZ
    Class B extends Class A - variables bX,bY,bZ
    Class C extends Class B - variables cX,cY,cZ

    at each level i recursively call the 'super' class method which
    processes my request and sets the variables at that level.
    this seems very longhand when i've got scores of variables to set from
    my form, each 'set' method called as appropriate after validation.
    is there a java pattern to handle this kind of scenario??

    thanks, j
     
    John McC, Aug 20, 2003
    #1
    1. Advertising

  2. John McC wrote:
    > my program needs to set many variables posted from a very complex form
    > by working its way up a multi-tiered type hierarchy
    > and setting the relevant variables that apply at that level. ie:
    > Class A - variables aX,aY,aZ
    > Class B extends Class A - variables bX,bY,bZ
    > Class C extends Class B - variables cX,cY,cZ
    >
    > at each level i recursively call the 'super' class method which
    > processes my request and sets the variables at that level.
    > this seems very longhand when i've got scores of variables to set from
    > my form, each 'set' method called as appropriate after validation.
    > is there a java pattern to handle this kind of scenario??


    Without seeing even pseudocode I can't guarantee it, but it sounds like
    you are doing much as I would do given the data structure you describe.
    Each class should take responsibility for the state it defines, and if
    you then construct a subclass it should handle only the state it adds
    while passing off the rest to the superclass.

    Now, ideally you would do this on a request by request basis, not on a
    variable by variable basis. Also, except for any universal validation
    rules (and perhaps even there) it makes sense that the class wherein the
    form values will be stored handles the validation rules for those values.

    There is no way around taking each parameter and assigning it
    individually to the appropriate field, but there may be other paradigms
    for doing so that appeal to you more than what you are doing now. For
    instance, if you are creating a new object to represent the form data
    then you could provide a constructor at each level that accepts a Map
    such as ServletRequest.getParameterMap() would provide. That Map can be
    passed by each such constructor to its superclass' constructor, and each
    one would pick out the parameters it wanted to use.

    Now, with all that said, I would probably be looking for ways to
    redesign the data structure. That would not fundamentally change the
    requirements for populating it from a user request, but it might make
    the actual code for doing so look more meaningful.


    John Bollinger
     
    John C. Bollinger, Aug 20, 2003
    #2
    1. Advertising

  3. John McC

    Harald Hein Guest

    "John McC" wrote:

    > my program needs to set many variables posted from a very complex
    > form by working its way up a multi-tiered type hierarchy


    You leave that to the compiler. Use public variables or methods. Your
    subclasses inherit these variables/methods and you can use them as if
    you had coded them in the subclass.
     
    Harald Hein, Aug 20, 2003
    #3
    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. CJ
    Replies:
    1
    Views:
    1,581
    Andrew Thompson
    Oct 29, 2004
  2. poofpoof
    Replies:
    2
    Views:
    610
    poofpoof
    Aug 13, 2004
  3. Pupeno
    Replies:
    4
    Views:
    308
    Raymond Hettinger
    Aug 2, 2006
  4. Siphiuel
    Replies:
    0
    Views:
    334
    Siphiuel
    Mar 7, 2006
  5. n00m
    Replies:
    12
    Views:
    1,119
Loading...

Share This Page