Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good design to execute the methods based on boolean values in Database in Java?

We have few rules, which are Implemented as methods in Java. But sometimes we need to bypass the rules. So for each rule, we have a boolean Indicator to indicate whether to execute or not. What can be a good design to map the methods to boolean values in Database and execute methods based on the boolean values.

Below is sample template

1 Rule1 true
2 Rule2 false
3 Rule3 true
4 Rule4 true

So, now I need to execute method1(), method3() and method4() respectively.

One Simple way can be using If(rulee == true) executeMethod();

Second is using a Switch to execute the cases (method calls)

Note: We may need to execute the methods in different locations(methods). So please dont consider that all the methods will be called from a single method.

Can I make use of AOP by any chance?

like image 330
omkar sirra Avatar asked Apr 13 '18 18:04

omkar sirra


People also ask

Which is better to use boolean or boolean?

It is recommended that you use the Boolean() function to convert a value of a different type to a Boolean type, but you should never use the Boolean as a wrapper object of a primitive boolean value.

Which of the wrapper classes have a boolean value () method?

Java provides a wrapper class Boolean in java. lang package. The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field, whose type is boolean.

What works on boolean method?

The boolean method converts the value of object1 to Boolean, and returns true or false. The exists method checks whether a value is present in object1. If a value is present, it returns Boolean true; otherwise, it returns Boolean false. The false method always returns Boolean false.


1 Answers

You could define the basic interface as

public interface Rule {
  boolean canExecute();
  void execute();
}

and convert the methods into Rule interface implementations. The boolean value in the database would map to canExecute() return value.

This would be a good idea if methods are becoming complex, there's more than a few of them and the parent class is starting to look like a God Object.

like image 145
Karol Dowbecki Avatar answered Sep 28 '22 07:09

Karol Dowbecki