Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Pattern for recursive variable setting over type hierarchy (http://www.velocityreviews.com/forums/t125835-pattern-for-recursive-variable-setting-over-type-hierarchy.html)

John McC 08-20-2003 03:38 PM

Pattern for recursive variable setting over type hierarchy
 
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 C. Bollinger 08-20-2003 05:33 PM

Re: Pattern for recursive variable setting over type hierarchy
 
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
jobollin@indiana.edu


Harald Hein 08-20-2003 07:49 PM

Re: Pattern for recursive variable setting over type hierarchy
 
"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.


All times are GMT. The time now is 05:12 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.