设计模式,在java中实现具有数百个if的其他业务规则

我必须用数百行以下代码实现某些业务规则

if this
      then this
else if
      then this
.
. // hundreds of lines of rules
 else
      that

我们是否有任何设计模式可以有效地实现这一点或重用代码,以便它可以应用于所有不同的规则。我听说过规范模式,它创建了如下内容

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);
}
}

然后实现Is,And,Or方法但我认为这不能省去我写的if else(可能是我的理解不正确)...

有没有最好的方法来实现这样的业务规则,有这么多的if else语句?

编辑:只是一个示例。A,B,C等是类的属性。除此之外,还有类似的许多其他规则。我想为此制作一个通用代码。

    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>

答案 1

您应该查看规则设计模式 http://www.michael-whelan.net/rules-design-pattern/。它看起来与您提供的示例代码非常相似,并且由一个基本接口组成,该接口定义了一个用于确定是否满足规则的方法,然后为每个不同的规则提供各种具体实现。据我所知,你的 switch 语句会变成某种简单的循环,只计算事物,直到你的规则组合得到满足或失败。

interface IRule {
    bool isSatisfied(SomeThing thing);
}

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

class RuleB: IRule {
    ...
}

class RuleC: IRule {
    ...
}

组成规则:

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);
}

对事物运行规则的一些服务方法:

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

用法:

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

var thing = new Thing();

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

这里也回答了这个问题:https://softwareengineering.stackexchange.com/questions/323018/business-rules-design-pattern


答案 2

策略模式在这里可能很有用。请检查将条件逻辑替换为策略


推荐