tags:

views:

133

answers:

5

Here's the code:

SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();

Why use that if you can use something more intuitive like:

SAXParser mySAXParser = new SAXParser();
+6  A: 

Because this allows the framework to provide you with different implementations of SAXParser in a transparent way. The implementation used may depend on configuration parameters, framework version etc. Using a factory allows the framework developers e.g. to replace an old, inefficient parser implementation with a different, better one, without breaking any client code.

Note that SAXParser is an abstract class, so you can't even instantiate it directly.

Péter Török
+4  A: 

SAXParser is an abstract class, you can't just instantiate the class directly.

The purpose of the Factory is to decouple the caller from having to know specifically which implementation of SAXParser is in use. It also allows the Factory to configure the parser if required before handing it back to the caller.

Paolo
If the SAXParser is an abstract class how is the Factory class able to instantiate it?
Emanuil
One more question if I may. Why is the mySAXParserFactory instantiated through a static .newInstance() method instead of through its constructor? Is this a Singleton?
Emanuil
Hi Emanuil, see my reply to these comments in a new answer
chiccodoro
+2  A: 

It allows you to determine the actual class of the Parser object you will be using in a different place than that particular line of code. Constructors must explicitly say exactly which type they construct; factory methods can hide that decision behind an interface.

Whether or not you need that flexibility depends on your application. Many people recommend adding that flexibility in every design just in case. Others find that attitude grotesquely over-engineered. You be the judge...

Kilian Foth
+1  A: 

"One more question if I may. Why is the mySAXParserFactory instantiated through a static newInstance() method instead of through its constructor?"

To avoid your code having compile time dependencies on a specific SAXParserFactory class. If you look at the javadoc for the newInstance() method, you will see that it allows you to configure different parser factory classes via a system property, a property file or the Services API.

"Is this a Singleton?"

No. The Javadoc says "This static method creates a new factory instance." (my emphasis)

"Ok, I'll ask a more general question. Why are some classes in Java instantiated through a constructor and others - through static methods (like the .newInstance() method mentioned here)?"

Different classes have different use-cases. For example, you would not want or need to implement your application so that someone could choose between using a hash map or a tree map for some data structure. However, you may well want someone to be able to select a different XML parser.

The purpose of the "factory object" and "factory method" patterns is to avoid having a hard dependency on specific object classes at object creation time. Sometimes this is a good thing, but in other situations the factory is just "dead weight". You need to learn ... by experience ... when to use factories, and when not to.

Stephen C
Ok, I'll ask a more general question. Why are some classes in Java instantiated through a constructor and others - through static methods (like the .newInstance() method mentioned here)?
Emanuil
+2  A: 

If the SAXParser is an abstract class how is the Factory class able to instantiate it?

Hi Emanuil. This is the point: The factory does not instanciate SAXParser but a certain subclass of it. Which subclass exactly it instanciates, as already mentioned by the others, is determined by the configuration / framework.

Why is the mySAXParserFactory instantiated through a static .newInstance() method instead of through its constructor?

Because the SAXParserFactory itself, again, is (IMHO) abstract. The newInstance() method creates a factory object from a concrete subclass of the SAXParserFactory. Actually, creating a concrete factory is the point where it is decided which implementation to choose.

This is called the "abstract factory pattern", see also:

chiccodoro
Brilliant! Thanks for the great answer!
Emanuil