2012-06-05 41 views

回答

6

沒有在性質沒有這樣的條件語句文件時,可能是你會寫一個包裝過Properties封裝你的邏輯

例子:

class MyProperties extends Properties { 


    /** 
    * 
    * 
    * 
    * @param key 
    * @param conditionalMap 
    * @return 
    */ 
    public String getProperty(String key, List<Decision> decisionList) { 
     if (decisionList == null) { 
      return getProperty(key); 
     } 
     Long value = Long.parseLong(getProperty(key)); 
     for (Decision decision : decisionList) { 
      if (Condition.EQUALS == decision.getCondition() && decision.getValue().equals(value)) { 
       return getProperty(decision.getTargetKey()); 
      } 
     } 
     return super.getProperty(key); 
    } 
} 

and

enum Condition { 
    EQUALS, GREATER, LESSER 

} 

class Decision { 
    Condition condition; 
    String targetKey; 
    Long value; 
    //accessor 

    public Condition getCondition() { 
     return condition; 
    } 

    public void setCondition(Condition condition) { 
     this.condition = condition; 
    } 

    public String getTargetKey() { 
     return targetKey; 
    } 

    public void setTargetKey(String targetKey) { 
     this.targetKey = targetKey; 
    } 

} 

所以現在,例如,如果你想讀的屬性文件,獲得的年齡段,如果是大於0和小於10讀kid

所以可能是你可以通過這樣的條件列表:

note:這個設計可以進行很多改進(不是很好的設計),它只是爲了說明如何包裝屬性和添加的東西,OP想要

+0

你能舉個例子嗎? – Piyush

+0

更新回答請檢查 –

3

正如@Jigar說,有一個屬性文件沒有條件邏輯。但你可以在那裏有兩條線,例如例如:

xyz.condition1 = abs 
xyz.condition2 = efg 

並且在您的代碼中訪問該屬性時,將正確的條件附加到該鍵。