A "Factory Method" is the name of a design pattern from the GoF book.
("Design Patterns:Elements of Reusable Object-Oriented Software," by
Erich Gamma, Richard Helm, Ralph Johnson, and Vlissides)
In many designs you have a situation where you need to get an instance of an
object, but you don't want the caller to have to specify the exact class
that's to be created.
For example, when you need an instance of a java.util.Calendar. The specific
instance you need depends on the current Locale. In most of the world you
get an instance of java.util.GregorianCalendar, but some locale's might have
configured to return some custom instance, say
org.islam.util.IslamicCalendar.
Ordinarily, to create an object, you do "new <classname>(...)", but then
you're dependent on the specific class - you have to know at compile time
whether you want a GregorianCalendar or an IslamicCalendar (or whatever). So
instead, you provide a method (usually static) that is invoked to create the
specific instance. So Calendar.getInstance() is a factory method for
creating instances of Calendar.
Another example is java.sql.DriverManager.getConnection(...), which is a
factory for creating instances of java.sql.Connection. It takes the JDBC url
and searches for a driver that can support it, and requests the driver to
create a connection and returns it. If you're using the JDBC-ODBC bridge,
you'll get one implementation of Connection, if you're using Oracle drivers
you'll get a different one.
The point is to hide the class hierarchy that implements an interface from
the client object that uses it.
They don't have to be static, either. In JAXP, you create an instance of a
javax.xml.parser.DocumentBuilderFactory with a static factory method
(DocumentBuilderFactory.newInstance()), but (as the name suggests)
DocumentBuilderFactory is itself a factory. Once you have one, you can
configure it to specify details of what kind of
javax.xml.parsers.DocumentBuilder you want (validating/non-validating,
namespace-aware/non-namespace-aware, etc.), then you get the actual
DocumentBuilder by calling the newDocumentBuilder() method on the factory.
The DocumentBuilder is a factory for org.w3c.dom.Document instances, too.
And org.w3c.dom.Document is a factory for org.w3c.dom.Element instances.
Cheers!
Mark
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html