commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bob mcwhirter <>
Subject Re: [CLI] Making Option Extensible
Date Wed, 24 Jul 2002 17:00:13 GMT
> > 	Option opt = new OptionBuilder()
> > 			.withShortOpt(...)
> > 			.withLongOpt(...)
> > 			.requiresArg()
> > 			.withDescription(...)
> > 			.create();
> > 
> > Where create() calls the constructor and does any other
> > initialization we need.  If nothing else, it's more readable
> > than constructor with 6 parameters.

> Is this considered a superior approach to the
> following example?
>  Option option = new Option();
>  option.setOpt( ... );
>  option.setLongOpt( ... );
>  option.setRequiresArg( ... );
>  option.setDescription( ... );

Yes, I think.

With the Builder approach, create() can throw an exception
if you haven't configured enough things:


	Option opt = new OptionBuilder().create();
		// puke!

> Even if we used the Builder pattern  I still think the 
> number of constructors and addOption methods
> should still be reduced to a simpler set.  Then the create() 
> would call the default constructor and all of the appropriate
> accessors.  Purely to make the API more readable for the 
> common case.

That'd be the plan.  Constructors goes 'protected', and create()
uses settors with a final call to validate() or something or
similar to make sure you've done at least some bare minimal usful 

We could even make the Builder recyclable.  Have create() clear
any internal state and ready it for producing another Option.

btw, I think patterns site has a further description of
the Builder patter (I could be wrong, I may have seen it on CiteSeer).
I've used Versant Object Database in the past, which used the Builder
pattern to create database sessions.  I find it much nicer than remembering
what parameter #7 is in a constructor, or forgetting to use a few
settors and not having them pop up until I attempt to actually do
something.  Knowing that create() will always produce a valid object
or an exception is comforting.


To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message