'Which design pattern is useful to evaluate conditions and return Boolean value

I would like to know best coding practice or design pattern which I can follow to achieve following task in Java: I need to enable check box in UI based on certain criteria/condition to check if those are satisfied in business layer which is written in Java.

There are 3 conditions which I need to evaluate, but I don't want to write if-else block inside single method to determine it. Also one or two condition will be common which will be used at other place too.

As of now, I am thinking to create one abstract class and create various implemention of it for conditions. Then create a list of those conditions class and evaluate in for loop one by one. Is that correct approach?



Solution 1:[1]

Actually, yes. You have 2 options: to write if-else or switch blocks (for small conditions that dont tend to grow) or to devide conditions into separate classes, that implement common interface, and combine them to reach desired condition (for lots of conditions and conditions, that tend to grow).

Maybe in your case it'll be easier to write if-else conditions? And, for example, extract common conditions into predicates and reuse them. Overall, don't do things to complex, if it's not needed))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Roman