I have to implement certain business rules with hundreds of lines of below code:
if this
then this
else if
then this
.
. // hundreds of lines of rules
else
that
Do we have any design pattern which can effectively implement this or reuse the code so that it can be applied to all different rules? I heard of Specification Pattern which creates something like the below:
public interface Specification {
boolean isSatisfiedBy(Object o);
Specification and(Specification specification);
Specification or(Specification specification);
Specification not(Specification specification);
}
public abstract class AbstractSpecification implements Specification {
public abstract boolean isSatisfiedBy(Object o);
public Specification and(final Specification specification) {
return new AndSpecification(this, specification);
}
public Specification or(final Specification specification) {
return new OrSpecification(this, specification);
}
public Specification not(final Specification specification) {
return new NotSpecification(specification);
}
}
And then the implementation of Is, And, Or methods but I think this cannot save me from writing the if-else (maybe my understanding is incorrect)...
Is there any best approach to implement such business rules having so many if-else statements?
EDIT: Just a sample example. A, B, C, etc are properties of a class. Apart from these, there are a similar lot of other rules. I want to make a generic code for this.
If <A> = 'something' and <B> = ‘something’ then
If <C> = ‘02’ and <D> <> ‘02’ and < E> <> ‘02’ then
'something'
Else if <H> <> ‘02’ and <I> = ‘02’ and <J> <> ‘02’ then
'something'
Else if <H> <> ‘02’ and <I> <> ‘02’ and <J> = ‘02’ then
'something'
Else if <H> <> ‘02’ and <I> = ‘02’ and <J> = ‘02’ then
'something'
Else if <H> = ‘02’ and <I> = ‘02’ and <J> <> ‘02’ then
'something'
Else if <H> = ‘02’ and <I> <> ‘02’ and <J> = ‘02’ then
'something'
Else if <H> = ‘02’ and <I> = ‘02’ and <J> = ‘02’ then:
If <Q> = Y then
'something'
Else then
'something'
Else :
Value of <Z>