2016-04-22 9 views
0

私はCommandFormatValidatorクラスを持っています。このクラスは、入力された文字列が事前定義されたパターンに適合するかどうかを調べます。クラスは、クラスの次のような形式につながっているものを、より多くの新しいパターン、実装された時間で:クリーンなコード、クラスを改善する方法

import java.util.StringTokenizer; 
import java.util.regex.Pattern; 

public class CommandFormatValidator { 

    private Pattern adlPatternAll = Pattern 
      .compile("^ACTV/(READ|ADD|DEL|RPL)/ADL.*"); 

    private Pattern adlPatternAddDefault = Pattern 
      .compile("^ACTV/ADD/ADL/(DFLTTY((/([A-Z0-9]{7})){1,5})|DFLMIN(/[0-9]{1,4}))"); 

    private Pattern adlPatternDeleteTtymailGeneral = Pattern 
      .compile("^ACTV/(DEL|READ)/ADL/TTYMAIL(/[A-Z0-9]{7})?"); 

//around 20 more pattern declarations... 

public void validate(Object payload){ 

     String command = (String)payload; 

     if (adlPatternAll.matcher(command).matches()) { 
      if (!adlPatternAddDefault.matcher(command).matches()) { 
       if (!adlPatternAddCityTty.matcher(command).matches()) { 
        if (!adlPatternAddCityFltTty.matcher(command).matches()) { 
         if (!adlPatternAdd.matcher(command).matches()) { 
          if (!adlPatternDelDefault.matcher(command).matches()) { 
           if (!adlPatternDel.matcher(command).matches()) { 
            if (!adlPatternDelCityFltTty.matcher(command).matches()) { 
             if (!adlPatternRpl.matcher(command).matches()) { 
              if (!adlPatternRead.matcher(command).matches()) { 
               if (!adlPatternReadCityFlt.matcher(command).matches()) { 
                if(!adlPatternAddTtymail.matcher(command).matches()) { 
                 if(!adlPatternDeleteTtymailGeneral.matcher(command).matches()) { 
                  if (!adlPatternDeleteTtymail.matcher(command).matches()) { 
                   throw new ServiceException(CommandErrors.INVALID_FORMAT); 
                  } 
                 } 
                } 
               } 
              } 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

を今私は、このクラスをクリーンアップしたいと思います。どのように私はそれを達成することができます誰も考えを持っていますか?私は私のケースに適用できるデザインパターンには特に感謝します。

+7

? – Gendarme

+1

それぞれの 'Pattern'インスタンスが特定のパターンを表す場合、' Pattern'クラスからサブクラス化して独自のパターンを作成し、抽象メソッドをオーバーライドすることでパターンごとの動作を定義します一致します。次に、すべてのパターンインスタンスを配列またはリストに入れ、パターンにマッチさせ、マッチする文字列の動作を呼び出す必要があります。 –

+0

@KeqiangLiサブクラス「パターン」?!? Yikes。あなたはとにかくできません。それは「最後」です。 – Andreas

答えて

4

これらをすべてアレイにリストし、配列を反復することができます。

ところでmatches()を使用する場合は、^のアンカーは必要ありません。あなたが最初のテストに!を逃した場合

は分からないのですが、ここではなくてです:

あなたがあれば、条件 `&&`代わりに、ネストされたのを使用していないのはなぜ
public class CommandFormatValidator { 

    private Pattern adlPatternAll = Pattern 
      .compile("^ACTV/(READ|ADD|DEL|RPL)/ADL.*"); 

    private Pattern adlPatternAddDefault = Pattern 
      .compile("^ACTV/ADD/ADL/(DFLTTY((/([A-Z0-9]{7})){1,5})|DFLMIN(/[0-9]{1,4}))"); 

    private Pattern adlPatternDeleteTtymailGeneral = Pattern 
      .compile("^ACTV/(DEL|READ)/ADL/TTYMAIL(/[A-Z0-9]{7})?"); 

    //around 20 more pattern declarations... 

    private Pattern[] adlAll = { adlPatternAddDefault 
           , adlPatternDeleteTtymailGeneral 
           //more 
           }; 

    public void validate(Object payload){ 
     String command = (String)payload; 
     if (! adlPatternAll.matcher(command).matches()) 
      return; 
     for (Pattern p : adlAll) 
      if (p.matcher(command).matches()) 
       return; 
     throw new ServiceException(CommandErrors.INVALID_FORMAT); 
    } 
} 
関連する問題