2016-12-14 77 views
0

我的應用程序有不同的客戶(20左右),每個客戶都有自己的*.properties文件與連接設置,屬性參數相同。區分多個屬性文件

目前,我已經爲每個客戶提供了一種自己的方法來讀取屬性並將其存儲在Customer中。與20個客戶,其充氣。我正在尋找更好的解決方案。

private final static Customer get_CustomerXXXX() { 

     final Properties p = new Properties(); 

      p.load(S.class.getResourceAsStream("customerXXX.properties")); 
      return new Customer (p.getProperty("PARAM1", p.getProperty("PARAM2", p.getProperty("PARAM3") 
    } 

    if(SPECIFIC_CUSTOMER.XXXX) { 
     customerSettings = get_CustomerXXXX(); 

    } else if(SPECIFIC_CUSTOMER.BBBB) { 
     customerSettings = get_CustomerBBBB(); 
    } 
+0

*請*格式化代碼時要注意。目前這一切都在這個地方。請記住,Stack Overflow的目標是成爲高質量問題和答案的存儲庫 - 不可讀的代碼會降低該目標。 –

回答

0
public class CustomerTest { 
    private String identifier; 

    public CustomerTest(String identifier) { 
     this.identifier = identifier; 
    } 

    public Properties getProerties() { 
    Properties p = null; 
    try { 
      p = new Properties(); 
      p.load(CustomerTest.class.getResourceAsStream("customer" + identifier + ".properties")); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return p; 
    } 
} 

這將是您在評論@OP建議的方式。

0

屬性參數都是相同

,如果你確信在未來還我會是一樣的。那麼你只能第一次閱讀屬性文件。從下次病房開始,使用從以前的屬性文件加載的值。

您可以在這裏結合Singleton和Factory Method設計模式。

你應該有一個方法,如下列:

private final static Properties getProperties(String idetifier) 
{ 
    Properties p = new Properties(); 
    p.load(S.class.getResourceAsStream("customer"+idetifier+".properties")); 
    return p; 
} 
+0

但連接設置有些不同 – Aelop

+0

只有連接設置不同,纔將連接設置放在客戶特定文件中。將通用值移至單獨的文件。 – Azodious

+0

'每個客戶都有自己的* .properties文件和連接設置,屬性參數名稱與他所說的相同。據我瞭解,只有參數名稱(鍵)沒有不同,但值是。這意味着每個porperties文件都有像param1 = xxx和param2 = yyy。 xxx和yyy可以更改,但param1和param2總是以該名稱存在。我可能是錯的,但是它已經告訴我們 – Aelop