Design Pattern to implement Business Rules with hundreds of if else in java

SCoder picture SCoder · May 31, 2013 · Viewed 60.1k times · Source

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 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 writing the if else(may be 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 similar lot of other rules.I wnat 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>

Answer

bingles picture bingles · Nov 20, 2017

You should check out the Rules Design Pattern http://www.michael-whelan.net/rules-design-pattern/. It looks very similar to the example code you gave and consists of a base interface that defines a method for determining if a rule is satisfied and then various concrete implementations per different rules. As I understand it, your switch statement would turn into some sort of simple loop that just evaluates things until your composition of rules is either satisfied or fails.

interface IRule {
    bool isSatisfied(SomeThing thing);
}

class RuleA: IRule {
    public bool isSatisfied(SomeThing thing) {
        ...
    }
}

class RuleB: IRule {
    ...
}

class RuleC: IRule {
    ...
}

Composition Rules:

class OrRule: IRule {
    private readonly IRule[] rules;

    public OrRule(params IRule[] rules) {
        this.rules = rules;
    }

    public isSatisfied(thing: Thing) {
        return this.rules.Any(r => r.isSatisfied(thing));
    }
}

class AndRule: IRule {
    private readonly IRule[] rules;

    public AndRule(params IRule[] rules) {
        this.rules = rules;
    }

    public isSatisfied(thing: Thing) {
        return this.rules.All(r => r.isSatisfied(thing));
    }
}

// Helpers for AndRule / OrRule

static IRule and(params IRule[] rules) {
    return new AndRule(rules);
}

static IRule or(params IRule[] rules) {
    return new OrRule(rules);
}

Some service method that runs a rule on a thing:

class SomeService {
        public evaluate(IRule rule, Thing thing) {
            return rule.isSatisfied(thing);
        }
    }

Usage:

// Compose a tree of rules
var rule = 
    and (
        new Rule1(),
        or (
            new Rule2(),
            new Rule3()
        )
    );

var thing = new Thing();

new SomeService().evaluate(rule, thing);

This was also answered here: https://softwareengineering.stackexchange.com/questions/323018/business-rules-design-pattern