首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在java中使用数百个if else实现业务规则的设计模式

在java中使用数百个if else实现业务规则的设计模式
EN

Stack Overflow用户
提问于 2013-05-31 12:15:48
回答 5查看 61.3K关注 0票数 36

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

代码语言:javascript
复制
if this
      then this
else if
      then this
.
. // hundreds of lines of rules
 else
      that

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

代码语言:javascript
复制
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等都是class.Apart的属性。还有很多类似的其他规则。我想为这个做一个泛型代码。

代码语言:javascript
复制
    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>
EN

回答 5

Stack Overflow用户

发布于 2017-11-21 01:24:26

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

代码语言:javascript
复制
interface IRule {
    bool isSatisfied(SomeThing thing);
}

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

class RuleB: IRule {
    ...
}

class RuleC: IRule {
    ...
}

组成规则:

代码语言:javascript
复制
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);
}

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

代码语言:javascript
复制
class SomeService {
        public evaluate(IRule rule, Thing thing) {
            return rule.isSatisfied(thing);
        }
    }

用法:

代码语言:javascript
复制
// 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

票数 24
EN

Stack Overflow用户

发布于 2013-05-31 12:30:12

策略模式在这里可能很有用。请查看Replace Conditional Logic with Strategy

票数 10
EN

Stack Overflow用户

发布于 2013-05-31 12:20:16

Drools这样的规则引擎可能会有所帮助。这不是一个设计模式,所以这可能不是你想要的答案。但国际海事组织,你应该考虑一下。这里有一篇关于when you should use a rules engine的很棒的文章。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16849656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档