0

I have class named: ComplexValidator that extends absract class Validator which have two methods:

Validate(Part part);
getAnswer():

I also have validators, lets name them A, B, C and D.

So

AValidator extends Validator
BValidator extends Validator
CValidator extends Validator
DValidator extends Validator

I am not in front of my code right not so I will use pseudo-code.

CValidator takes different parameter than rest of it, A B and D uses part to get filename etc, but CValidator uses numberOfFiles (which are increased in loop (for part p: multipart) so after every time loop is repeated numberoffiles is increased so I can compare it with maxNumberOfFiles field).

Sadly I didnt know how to make abstract class that will take any parameter to method so all methods must take Part part. Cvalidator doesnt use it and I had to make field numberOfFiles static so I can get access to it.

Is there any way to make those validators takes no parameters but no using static? Is there any way to make abstract class the way that child classes will be able to change arguments it take? And if it takes other arguments HOW can I loop it all when I have:

List <Validator> validators = new ArrayList<>();

in my ComplexValidator.

and then I add all child validators to it and loop over them like that:

for (Validator v: validators){
validate(part);
}
1
  • 4
    Please wait until you can post the code, and post it. Commented Jun 12, 2015 at 15:04

2 Answers 2

3

The types of the parameters of an overriden method must be the same as the original method.

To face your problem I would create a custom class that wraps all the different parameters that you might want to pass to the validate function.

Something like that

class Wrapper{
  Part param1;
  File param2;
  File param3;
}

class Validator{
  void validate (Wrapper wrapper);
}


class ValidatorA extends Validate{
 void validate (Wrapper wrapper){
    //use wrapper.part... 
 }
}

class ValidatorC extends Validate{
 void validate (Wrapper wrapper){
    //use wrapper.file... 
     }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I will give it a try as soon as I can! Thanks!
But it makes me wonder. Is wrapper correct name for it? Is is valid wrapper class? From what I read wrapper classes are used for primitives to use them as objects, shouldn't then it be named different? Or am I wrong?
You may want to change the parameter Wrapper wrapper to Object... params; creating a method "with the same name but different parameters" is called method overloading.
I need it to be called same way, so I can loop over it so overloading is not the answer. Given class works fine - I just think if is it correct way to use wrapper name?
The solution provided by Binkan Salaryman is also good. Anyway, the Wrapper class wraps the parameters that you pass to the method, so Wrapper could be a reasonable name
1

You may want to use java reflection. With a Class you can either getMethods and loop throught the methods and getParameterTypes of each method or if you know in advance the types of the method you wish you can getMethod (without s) and provide an array of type.

In your case I would go to the first method and depending on the presence of the second parameter (number of files), invoke the method the good way (with all the parameters needed).

2 Comments

I highly recommend staying away from reflection if possible. It slows down your program when used excessive and makes the code hard to understand.
I agree but in this context, the call to the method doesn't appear to be done "excessively". Reflection is a perfect valid answer to this problem, instead of having dummy parameters for all methods which is for me a bad design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.