设计模式实现业务规则与数以百计的其他在java [英] Design Pattern to implement Business Rules with hundreds of if else in java

查看:217
本文介绍了设计模式实现业务规则与数以百计的其他在java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实施一些业务规则,数百行以下代码

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

然后执行Is,And,或方法但我认为这不能救我写这个else(可能是我的理解是不正确的)...

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)...

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

Is there any best approach to implement such business rules having so many if else statements?

编辑:只是一个示例.A,B,C等是一个类的属性。从这些属性中有类似的很多其他规则。 wnat为此制作通用代码。

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>


推荐答案

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

Strategy pattern could be useful here. Please check Replace Conditional Logic with Strategy

这篇关于设计模式实现业务规则与数以百计的其他在java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆