dcsimg
What are Factory methods and when do we use them?....
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   SureshChandra_Medasani
Posted On:   Thursday, February 22, 2001 01:32 PM

What are Factory methods and when do we use them?.

Re: What are Factory methods and when do we use them?....

Posted By:   Christopher_Schultz  
Posted On:   Thursday, April 5, 2001 09:41 AM

The "Factory" pattern is one where you ask an object to create an instance of an Interface for you, no matter what type is actually is.



For instance, lets say that you want to use an XML (SAX) parser, but you don't care who wrote the parser.



Your first option is to do this:




SAXParser saxp = new my.verndors.sax.parser.Class();
.
.
/* use the parser */
.
.



Unfortunately, that locks you down to using a particular parser. If you use a factory method, you can write your code like this:




SAXParser saxp = ParserFactory.createSAXParser();
.
.
/* use parser */
.
.



You don't care if it's Oracle's parser, or Apache's parser or XML4j or W3C or whatever! You just know that you want a SAXParser.



Look in the 'patterns' forum for more information on the Factory pattern.



-chris
About | Sitemap | Contact