[jdom-interest] Re: Factories for Element creation

Patrick Dowler Patrick.Dowler at nrc.ca
Wed Nov 1 09:26:58 PST 2000


On Tue, 31 Oct 2000, David Churchville wrote:
> 1) Custom class approach:
> 
> SAXBuilder myBuilder = new SAXBuilder();
> 
> myBuilder.setElementClass("com.bar.foo.CustomElement");

It's nice to do it this way because then the String for the class name
can come from anywhere: command line, config file, database, etc.

> 2) Factory approach:
> 
> SAXBuilder myBuilder = new SAXBuilder();
> 
> myBuilder.setElementFactory( new MyElementFactory());
> 
> public class MyElementFactory implements JDOMElementFactory {
> 
> public Element createElement() { return new CustomElement() ...}
> 
> public Element createElement( arg1, arg2.) { ...}
> 
> ...
> 
> }

Another alternative is to write SAXBuilder (for example) such that it creates
Elements using a single protected method (overloaded for various args).
Then a user could just subclass SAXBuilder to get MySAXBuilder and
override those methods. It is usually possible to do this sort of thing such
that only 1-2 methods need to be customized. It does kind of make
SAXBuilder a factory of sorts, but for internal use only...

The downside is that if you want to use your element type with various
builders and filters, you probably have to do more work than with the
traditional factory design.

Factory is OK as long as it is optional - which I think you are advocating.

-- 

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list