Deepak
Deepak

Reputation: 2102

Design Pattern Questions in Java

I have a Design pattern Question in Java/J2EE

Problem Statement::
Suppose there are 2 files as below

XYZ.properties file --> name = value pair
abc.xml file --> attribute=value pair.

Both the above files have <name,value> pairs.

The question is "Design a component/jsp page such that you can read,edit,save the name,value pairs of either a Property file or xml file.?"
So which Design pattern would you suggest i should be using in Java.?
The name,values pair are then fetched and displayed in another jsp page where i should be able to read,edit,save the name,value pairs such that
1>read operation results in reading the name,value pair
2>Edit operation results in editing the name,value pair
3>Save operation results in saving the value for the corresponding name,value pair in database and consequently updates either the property/xml file.

**My Initial Analysis** : i would use a Factory design pattern since i have 2 files which have name,value pairs and at run time decide which one to choose depending on the name of file invoked,Once done i would pass the parameters to jsp file for Read,edit and save operation.Save would save the name,value pair to Database but i dont know how will it update the corresponding value for that name in either the property/xml file as well.

Is my Understanding correct ? If not please provide a design/solution in java for the same,such that READ,EDIT,SAVE operation for pairs in either ".properties file" or ".xml file" are carried out?

Upvotes: 0

Views: 831

Answers (3)

Nishant
Nishant

Reputation: 55866

I think Factory pattern is correct. Make abstracted getInstance(fileName), read, create, update, remove methods.

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.


Ideally, I would create a MyFileFactory class that looks like this

  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();
  }

have these classes to implement the methods

  public class XMLFileEditor extends MyFileFactory{
   ...
   private File f;
  }

and

  public class PropsFileEditor extends MyFileFactory{
   ...
   private File f;
  }

And I think you are good to go.


If you want to save in database as well as in the file:

 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
 }

Upvotes: 0

Amanpreet
Amanpreet

Reputation: 526

The Factory Pattern would do the job.

Create an abstract class with abstract read, modify, save methods and getter and setter for key and value

create two separate classes for .properties and .xml implementation who extend the abstract class.

Whenever you want to save check for the instance of the class and call the appropriate method.

Upvotes: 1

Kennet
Kennet

Reputation: 5796

This looks like homework. Without giving away too much, Properties can read/write both .properties and .xml files, more info on http://download.oracle.com/javase/7/docs/api/java/util/Properties.html.

Upvotes: 1

Related Questions