Creating funny objects

H

hopkins

After reading some of the Java/XML tutorials on the Sun site I was
puzzled by the way in which they create objects:

// Use an instance of ourselves as the SAX event handler
DefaultHandler handler = new Echo01();
// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
// Set up output stream
out = new OutputStreamWriter(System.out, "UTF8");

// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File(argv[0]), handler);

} catch (Throwable t) {
t.printStackTrace();

In particular, why are objects created like this

SAXParserFactory factory = SAXParserFactory.newInstance();
...and
SAXParser saxParser = factory.newSAXParser();

I have always been taught to creat object in the form

Person newPerson = new Person();

This way where you seem to call a different method other than the
constructor when the object is created confuses me and I cant really
understand why you'd want to do this. Secondly as the two lines in
question dont seem to have the "new" keyword does this mean they are
not created on the heap and are stack based like in C++?

Thanks in advance!
 
P

Paul Tomblin

In a previous article, (e-mail address removed) (hopkins) said:
In particular, why are objects created like this

SAXParserFactory factory = SAXParserFactory.newInstance();
...and
SAXParser saxParser = factory.newSAXParser();

I have always been taught to creat object in the form

Person newPerson = new Person();

This way where you seem to call a different method other than the
constructor when the object is created confuses me and I cant really
understand why you'd want to do this. Secondly as the two lines in
question dont seem to have the "new" keyword does this mean they are
not created on the heap and are stack based like in C++?

There is a book called "Design Patterns" which explains all this. You
should get it.

Basically, a "factory" is used so that the programmer can override it to
create a (or more than one different) subclass of SAXParser. If I have a
class called "fooParser" and another class called "barParser", both of
which are subclasses of SAXParser, I only have to make the factory return
fooParsers or barParsers as the circumstances may merit, instead of
SAXParsers, and all the other code stays the same. The factory method
"newInstance" would do the appropriate "new fooParser" or "new barParser"
inside it.

For instance, in the application I'm working on right now, I have a class
called "Playlist". I have subclasses called "AdvertisingPlaylist",
"FeaturePlaylist", "TrailerPlaylist" and "SnipePlaylist".
"FeaturePlaylist" has two subclasses "FilmPlaylist" and
"DigitalFeaturePlaylist". I have a PlaylistFactory with a method
"newPlaylist(int type)", which does a
Playlist n = null;
switch(type)
{
case TYPE_ADVERTISING:
n = new AdvertisingPlaylist();
break;
case TYPE_TRAILER:
n = new TrailerPlaylist();
break;
...
}
return n;

And so where-ever I need a new Playlist, I call on the factory to make it
rather than having that switch statement in each place.
 
M

Michael Borgwardt

hopkins said:
In particular, why are objects created like this

SAXParserFactory factory = SAXParserFactory.newInstance();
...and
SAXParser saxParser = factory.newSAXParser();

I have always been taught to creat object in the form

Person newPerson = new Person();

This way where you seem to call a different method other than the
constructor when the object is created confuses me and I cant really
understand why you'd want to do this.

This is known as a factory method. Paul gave an example why one might
want to do this.

Basically, it's more flexible than a constructor, because a constructor
can only "return" an instance of the concrete class, while a factory can
declare an abstract class or an interface as its return type and return
an arbitrary subclass or intrface implementation. Note that in your
code, SAXParser is an abstract class, so it's not possible to instantiate
it with "new".

Secondly as the two lines in
question dont seem to have the "new" keyword does this mean they are
not created on the heap and are stack based like in C++?

No. The factory method will use "new" in some way, directly or indirectly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top