36

以下の数百行のコードで特定のビジネスルールを実装する必要があります

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>
4

6 に答える 6

10

ここでは戦略パターンが役立ちます。条件付きロジックを戦略に置き換えるを確認してください

于 2013-05-31T04:30:11.947 に答える
6

Command パターンまたはFactory パターンを使用できます。

コマンド パターンを使用すると、新しいオプションを追加すると無限に大きくなる面倒な switch/if ブロックを置き換えることができます。

public interface Command {
     void exec();
}

public class CommandA() implements Command {

     void exec() {
          // ... 
     }
}
// etc etc

次に、Map<String,Command>オブジェクトを作成し、それに Command インスタンスを設定します。

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

次に、if/else if チェーンを次のように置き換えることができます。

commandMap.get(value).exec();

Factory Patternでは、if/switch を Factory に含めます。これにより、醜さが処理され、大量の if が隠されます。 Factory Pattern のコード例

于 2013-05-31T04:27:10.413 に答える
1

コードを読みやすくしたり、保守性を向上させたりするには、デザイン パターンを使用する必要がありますが、そのような多数の条件を本当に評価する必要がある場合は、IF ステートメントを使用することは避けられません。

別の観点から問題を見ることを検討し (例: 問題を解決するために本当に 100 の条件文が必要か?)、アルゴリズムを変更または改善しようとします。

式言語は、各 IF ステートメントを表す文字列をプログラムで作成し、このツールを使用して結果を評価できるため、ある程度の助けになりますが、各条件に関連付けられた特定のロジックの実行に関連する問題を解決する必要があります。

于 2013-06-01T18:07:58.643 に答える
1

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)
于 2015-02-02T09:01:32.097 に答える