以下の数百行のコードで特定のビジネスルールを実装する必要があります
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 ステートメントが非常に多いこのようなビジネス ルールを実装するための最善の方法はありますか?
EDIT : サンプルの例です。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>