Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

3/24/23, 2:05 PM java - Business logic validation patterns & advices - Stack Overflow

Business logic validation patterns & advices


Asked 6 years, 8 months ago Modified 2 years, 1 month ago Viewed 19k times

I have two layers of validation in my application. First is entity validation performed by bean
validation API (e.g. required fields). The second level is business logic validation. For example, user
20 has a post. User can delete a post only if he is a creator of this post and post rating < 50. So I
have to do something like this:

if (post.getCreator().equals(session.getUser())) {
if (post.getRating() < 50) {
postRepository.delete(post);
} else errors.add(400, "Cant delete post with rating 50 or higher")
} else errors add (400, "You should be owner of the post")

I don't like this way as this conditionals are reused and I have to duplicate code. Moreover, if
number of conditionals is greater than 5 or so it becomes unreal to read and understand the
code.

Moreover, standard Spring Validator won't be very helpful as I have to maker different validation
for one entity on different actions (delete and update for example)

So I'm looking for a way to do this in a smarter way (pattern maybe) and I would be very grateful
if someone could give me a hint.

Thank in advance!

java spring validation design-patterns

Share Improve this question Follow edited Jul 4, 2016 at 12:48 asked Jul 4, 2016 at 12:12
Rahul
1,647 2 17 32

Sorted by:
2 Answers
Highest score (default)

You can use the strategy pattern.

27 Each condition can be modelled as a function that takes a post and a session context and might
return an error:

https://stackoverflow.com/questions/38184481/business-logic-validation-patterns-advices 1/3
3/24/23, 2:05 PM java - Business logic validation patterns & advices - Stack Overflow

Post -> Session -> Optional<String>

You could represent this with an interface:

@FunctionalInterface
public interface ValidationCondition {

Optional<String> validate(final Post post, final Session session);


}

So for example:

public class CreatorValidation implements ValidationCondition {

public Optional<String> validate(final Post post, final Session session) {


if (post.getCreator().equals(session.getUser()) {
return Optional.empty();
}
return Optional.of("You should be the owner of the post");
}
}

You can then store every validation in a list:

final List<ValidationCondition> conditions = new ArrayList<>();

conditions.add(new CreatorValidation());
conditions.add(new ScoreValidation());
// etc.

Using the list, validations can be applied in bulk:

final List<String> errors = new ArrayList<>();

for (final ValidationCondition condition : conditions) {


final Optional<String> error = condition.validate(post, session);
if (error.isPresent()) {
errors.add(error.get());
}
}

Using Java 8 lambdas, you could declare these inline:

final ValidationCondition condition = (post, session) -> {


// Custom logic
});

Share Improve this answer Follow edited Feb 19, 2021 at 13:37 answered Jul 4, 2016 at 18:57

https://stackoverflow.com/questions/38184481/business-logic-validation-patterns-advices 2/3
3/24/23, 2:05 PM java - Business logic validation patterns & advices - Stack Overflow

sdgfsdh
32.3k 24 126 228

Strategy pattern is the solution in my opinion. I will give you a very simple example. Lets say we
have two kinds of credit cards, Visa and Mastercard. The logic to perform payment operation is
3 the same for both cards, but card number validation is different. So, by passing VisaStrategy
object through a workflow does the same logic and operations as we would pass
MastercardStrategy, except one thing - card number validation, which is done inside each defined
Strategy class, so you do not have any "if else" stuff in your code at all. Each Strategy class is now
responsible for one and only one type of card validation. If you look for flexible and easy to
maintain code structure - use Strategy design pattern.

Share Improve this answer Follow edited Jul 4, 2016 at 21:38 answered Jul 4, 2016 at 21:26
Geka P
557 1 5 21

3 What if the parameters of validation method varies by condition? Lets say Visa cardnumber is an integer
and also has a 4 digit code, but master card number is a one 16 digit number? – ahmet Mar 5, 2018 at 8:22

https://stackoverflow.com/questions/38184481/business-logic-validation-patterns-advices 3/3

You might also like