Hi,
In short, here is my case:
I want to make a function to read XML file. In addition, I want to find a way to save as much memory for my application.
I don't know which ways is better for me:
Case 1:
Class XmlParser
{
//some kinds of parser
private Xml parser = null;
private static XmlParser instance;
public XmlParser()
{
parser = new Xml();
}
...
public static void getInstance()
{
if(instance == null) instance = new XmlParser();
return instance;
}
In this case, I create an instance to point to this class, so it won't be create again when I call the XmlParser
e.g XmlParser.getInstance().parse(fileUrl);
Whenever I need to use a parser, I just call a method in it and pass the file URL. It will create a new streamInput to pass to the parser;
Note: the "parser = new Xml()" is called only one time when the variable is created first time.
Case 2:
public class XmlParser
{
private Xml parser = null;
public XmlParser(String fileUrl)
{
//create input stream to the file and pass to the new parser
}
// some methods which can be called to start parsing
....
}
In this case, when I want to use the Parser, I must create a new instance of this class.
e.g XmlParser parser = new XmlParser("abc.xml");
An_Object a = parser.parse();
Could you please compare these two cases for me. Which one is the better way when I want to use the xml parser and want to save memory too (like optimization, maybe).
Note: I am using pull parser. About parsing xml in my application, the parser will be called when the app is started (only 1 time) and when a user changes the configuration of the app (e.g. he changes configuration of the app 1 times = 1 call to the parser, 2 time = 2 calls to the parser, etc.).
Thank you.

Reply With Quote

