How did you reconcile using static factory methods and taunt?
Many people will simply say: do not use static factory methods, use DI instead.
Well, sometimes you cannot escape the static factory methods. Consider the following use cases that should be familiar:
Imagine you have a class called Option, as in scala. You cannot avoid using the static factory method if you want to reuse the same instance for all missing values.
Once you go new Option(null), you create a new option object, you cannot return the same object again and again.
A similar use case is Integer.valueOf()that will reuse integer objects for values below 128. It is impossible to do without using the static factory method.
Another advantage is that factory methods are more visual than new.
So, how do you guys deal with using static factory methods and at the same time want to use inheritance and mocks?
Thanks.
U mad source
share