Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think Factory pattern is correct. Make abstracted <code>getInstance(fileName)</code>, <code>read</code>, <code>create</code>, <code>update</code>, <code>remove</code> methods. </p> <p>As a suggestion, you might want to return implemented instance after checking into the file content -- whther it's XML (by checking XML header or presence of root tag) or it's a properties file.</p> <hr> <p>Ideally, I would create a MyFileFactory class that looks like this</p> <pre><code> public class MyFileFactory{ public static final MyFileFactory getInstance(String filename){ //read the file header or first line to guess what instance to call int fileType = getFileType(fileName); if(fileType == MyFileFactory.XML_FILE) return new XMLFileEditor(fileName); else return new PropsFileEditor(fileName); } public abstract readFile(); public abstract writeToFile(); public abstract editFile(); } </code></pre> <p>have these classes to implement the methods</p> <pre><code> public class XMLFileEditor extends MyFileFactory{ ... private File f; } </code></pre> <p>and</p> <pre><code> public class PropsFileEditor extends MyFileFactory{ ... private File f; } </code></pre> <p>And I think you are good to go.</p> <hr> <p>If you want to save in database as well as in the file:</p> <pre><code> writeToFile(String new_Node_or_property){ //parseNode converts the property or XML node to an object MyNode node = MyNode.parseNode(new_Node_or_property); MyFileDAO.insert(node);//write a code that parse // write here code to append in the file based on what // filetype implentation it is -- XML, .properties, YAML whatever } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload