2016-08-22 87 views
0

我對JAVAFX很新穎。我剛開始考慮如何存儲用戶文件,在我的情況下我不想使用XML。我正在創建一個以前在perl中完成的工具的新版本。用戶文件是基於文本的,並用專有定義完成。用於存儲用戶數據的JAVAFX體系結構

舉個例子

PROJECT_NAME some_device; 
DATA_BUS_WIDTH 32; 

LINE_TYPE_A name_A mask:0xFFFFFFFF default:0x00000000 "Some documentation about this line type 
SUB_LINE_TYPE_A_0 sub_name_0 PROP0 "Some documentation about what the sub line does"; 
SUB_LINE_TYPE_A_1 sub_name_1 PROP0 "Some documentation about what the sub line does"; 
LINE_TYPE_B name_B PROP_2 Size:0x1000 "Some documentation about this line type - important Linetype B has different properties than the previous line type A" 
SUB_LINE_TYPE_B_0 sub_name_0 "Some documentation about what the sub line does"; 
LINE_TYPE_C name_C Other PROPs "And more documentation" 

什麼,我想這樣做是創建一個文檔類,然後創建一個數組,這將持有每一條線。但問題是,文檔類將擁有三個(或更多)類型的對象的對象數組。一個LINE_TYPE_A,LINE_TYPE_B等對象,每個類型有不同的屬性。我很熟悉創建一種類型的對象的數組。創建一個多種類型的數組,對我來說似乎很奇怪,但似乎應該有一種方法。當我瀏覽列表時,我必須能夠看到每個項目,並說,你是TYPE A還是TYPE C,所以我可以適當地處理數據。

這是創建自定義文檔格式的正確方法嗎?還是有什麼我應該做的?但是,我仍然希望遠離XML。

+0

在這個問題中沒有什麼特定的javafx。 –

回答

0

有幾種方法你可以去構建這樣的:

A)定義的數據結構,說DataLine,來保存數據。這將包含在特定線路的所有信息:TYPEname等,然後繼續與你想要做什麼:

class Document { 
    //array or list or some other collection type of `DataLine` 
    DataLine[] lines; 

    void doStuff() { 
     for (DataLine line : lines) { 
      // line.getType(), line.getName(), etc 
     } 
    } 
} 

B)定義一個基於繼承結構,將常見的隔離場/查詢方法,例如

// abstract class if you have some common methods or interface 
abstract class DataLine { 

    abstract DataType getType(); 
} 

// some specific data that belongs to only TypeA 
class DataLineTypeA extends/implements DataLine { 

} 

class Document { 
    //array or list or some other collection type of `DataLine` 
    DataLine[] lines; 

    void doStuff() { 
     for (DataLine line : lines) { 
      // can also check with getType() if you have it stored 
      if (line instanceof DataLineTypeA) { 
       DataLineTypeA typeA = (DataLineTypeA) line; 
       // do stuff with typeA specific methods 
      } 

      // etc. 
     } 
    } 
} 

最後,你可以創建自己的數據分析程序,如果你有你的數據的正式定義,或使用像JSON的中間格式。或者,您可以使用默認的Java serialization mechanism來使數據持久。

相關問題