Re: Another JUnit scenario
Lew wrote:
A (usually - always the disclaimer) better pattern is to include the
Locale as an argument to the factory method. Static state is an
antipattern.
public class LocalizationUtils
{
/** Don't forget the private constructor! */
private LocalizationUtils(){}
/**
* Foo factory with default Locale.
* @return Foo instance with default Locale.
*/
public static Foo getFooInstance()
{
return new SubtypeOfFoo();
}
/**
* Foo factory with custom Locale.
* @param locale custom Locale.
* @return Foo instance with custom Locale.
*/
public static Foo getFooInstance( Locale locale )
{
return new SubtypeOfFoo( locale );
}
}
Remember, the only hard and fast rule of programming is that there are
no hard and fast rules of programming.
Arne Vajh??j wrote:
I would prefer a non static field + getter + setter in the factory
over the arguments to the get instance methods.
As long as you don't need the thing to be thread safe that's fine, too. Even
better, usually, is a non-static final field set in the factory's constructor
with only a getter. You instantiate a new instance of the factory each time
you need a different 'Locale' (or whatever the attribute(s) is/are).
IF you need to get many objects different places in the code
then configuring the factory once may require less code and
may work better if the factory is retrieved via something
like Spring.
I have a hard time grokking Spring.
--
Lew