Pattern for recursive variable setting over type hierarchy

J

John McC

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
 
J

John C. Bollinger

John said:
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
(e-mail address removed)
 
H

Harald Hein

John McC said:
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.
 

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

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top