2016-04-22 47 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子類'Pattern'?!?讓人驚訝。那麼,你無法反正。這是'最終'。 – Andreas

回答

4

您可以將它們全部列出在一個數組中,並迭代該數組。

BTW:使用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); 
    } 
}