Reputation: 518
I'm writing implementations of a specific Validator interface. It is designed in a way that it is expected to throw a specific exception when the validation fails. I have no possibility to change this design.
Each implementation obtains validation input from the input data set in a different way and validates different number of data sets. However for each validation input the actual validation code is the same. That's why I decided to write a class with validate method to place this common code and call it from each Validator implementation whenever needed. The class is something like this:
public class SpecificValidationService {
public void validate (Condition condition1, Condition condition2) {
if (conditionNotMet(condition1, condition2)) {
throw new SpecificException();
}
}
}
The problem is that having a service that has only one public method (that is void) seems wrong to me. Generally I would expect a service to have a few methods that are not void. Am I right here? What should be a better name? Remember that I cannot use 'validator' as it can be confused with the Validator implementations. Or maybe my approach is wrong altogether?
Upvotes: 1
Views: 663
Reputation: 976
There is no concrete definition of a service class. Infact a service can mean different thing in different languages/frameworks. In Java, generally any class that implements some Business logic can be called a service. There is nothing wrong with a service with a single void function. However the SpecificValidationService class looks to me more like a utility class and can be kept in service layer. You may call it SpecificValidationUtility in that case.
I would like to share this answer https://softwareengineering.stackexchange.com/a/343213 for an explaination on service layer.
Upvotes: 1
Reputation: 76905
Let us first clarify that there is absolutely nothing wrong with void
methods and there is absolutely nothing wrong with classes only having void
methods by themselves.
The very question of whether classes having only void
methods should be avoided is wrongly put. The question you should ask yourself about a plan of yours, or a class
of yours looks like this:
Is this class meeting my requirements?
If the answer is yes, then it is not interesting whether it only has a void
method or not. If the answer is no, you will need to ask yourself the next question:
How would this class look alike if it was meeting my requirements and what is the difference between what it should be like and what it is?
And since you are asking now the good questions, you will have a higher chance of finding the good answers.
I understand that you need an interface
for your validation and you want to implement at least a method. This means that you need an abstract class
to implement
your interface
.
public abstract class AbstractValidator implements Validator {
public void validate (Condition condition1, Condition condition2) {
if (conditionNotMet(condition1, condition2)) {
throw new SpecificException();
}
}
}
and then for each specific validator, inherit from AbstractValidator
. Also, document at Validator
that it is not meant to be implemented by anything else than AbstractValidator
, so other people, who might find your interface
will not make the mistake of implementing it separately.
https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
Upvotes: 0