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

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

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,或者方法,但我认为这不能救我写if if(可能是我的理解是不正确的)…

是否有任何最佳方法来实现具有如此多if else语句的业务规则?

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

  If  = 'something' and  = 'something' then If  = '02' and   '02' and   '02' then 'something' Else if   '02' and  = '02' and   '02' then 'something' Else if   '02' and   '02' and  = '02' then 'something' Else if   '02' and  = '02' and  = '02' then 'something' Else if  = '02' and  = '02' and   '02' then 'something' Else if  = '02' and   '02' and  = '02' then 'something' Else if  = '02' and  = '02' and  = '02' then: If  = Y then 'something' Else then 'something' Else : Value of  

战略模式在这里很有用。 请检查使用策略替换条件逻辑

您可以使用命令模式工厂模式

命令模式可用于替换繁琐的开关/ if块,当您添加新选项时,这些块往往会无限增长。

 public interface Command { void exec(); } public class CommandA() implements Command { void exec() { // ... } } // etc etc 

然后构建一个Map对象并用Command实例填充它:

 commandMap.put("A", new CommandA()); commandMap.put("B", new CommandB()); 

然后你可以用以下代码替换你的if / else if链:

 commandMap.get(value).exec(); 

工厂模式中,您可以在工厂中包含if / switch,它可以处理丑陋并隐藏丰富的ifs。 工厂模式的示例代码

可能有用的东西是像Drools这样的规则引擎。 这不是一种设计模式,所以这可能不是你想要的答案。 但IMO,你应该考虑一下。 这是一篇关于何时应该使用规则引擎的精彩文章。

设计模式可以帮助您使代码更具可读性或提高其可维护性,但如果您确实需要评估此类数量的条件,则无法避免IF语句。

我会考虑从其他角度看问题(例如:我真的需要一百个条件语句来解决问题吗?)我会尝试改变或改进算法。

表达式语言可以提供一些帮助,因为您可以通过编程方式创建表示每个IF语句的字符串并使用此工具评估结果,但您仍然必须解决与执行与每个条件关联的特定逻辑相关的问题。

使用python进行简单的策略演示:

 class Context(object): def __init__(self, strategy): self.strategy = strategy def execute(self, num1, num2): return self.strategy(num1, num2) class OperationAdd(object): def __call__(self, num1, num2): return num1 + num2 class OperationSub(object): def __call__(self, num1, num2): return num1 - num2 if __name__ == '__main__': con = Context(OperationAdd()) print "10 + 5 =", con.execute(10, 5) con = Context(OperationSub()) print "10 - 5 =", con.execute(10, 5) 

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

 interface IRule { bool isSatisfied(SomeThing thing); } class RuleA: IRule { public bool isSatisfied(SomeThing thing) { ... } } class RuleA: 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